Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### New Features

- New `codegraph-vba dead-code` command lists code nothing in your project reaches, and on Access it leaves out event procedures, functions your forms' expressions or SQL call, `Implements` members and Dysflow test modules, while public procedures in standard modules are listed apart as "public, possibly external" (#344).
- VBA calls and property reads now reach the right class through more kinds of receivers: a `Public` variable of a standard module used from any other file, an `Object` variable `Set` from a factory function in another module, `Me.Field.Member`, and chains such as `a.Hijo.IDCalc` typed through a `Property Get` or public field, `With` blocks included; a receiver whose type cannot be deduced still links nothing (#345).

### Fixes

- VBA calls and property reads/writes now link even when their case differs from the declaration, such as `a.idcalc` for `IDCalc`, or a call on a variable declared `As EXPEDIENTE` reaching the class `Expediente`; an ambiguous match is still left unlinked. (#346)
- After `codegraph-vba sync`, a VBA property access or saved-query name that had no target now links once a later edit adds the missing `Property Get`/`Let`/`Set` or query, in any case, without re-indexing the file that uses it. Fixes part of #336. (#346)


## [3.1.0] - 2026-11-29

### Highlights
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ codegraph-vba impact <symbol> # Analyze what code is affected by changin
codegraph-vba implementations <symbol> # List the types that implement or extend a type (--depth, --json)
codegraph-vba affected [files...] # Find test files affected by changes (see below)
codegraph-vba cycles [path] # List circular imports between files (--json); exits 1 when there are any
codegraph-vba dead-code [path] # List code nothing reaches, VBA-aware (-p, --limit, --json; see below)
codegraph-vba daemon # Manage background daemons — pick one to stop (alias: daemons)
codegraph-vba daemon stop --path <root> # Release one project's daemon and index locks (non-interactive)
codegraph-vba telemetry [on|off] # Show or change anonymous usage telemetry
Expand Down Expand Up @@ -790,6 +791,28 @@ if [ -n "$AFFECTED" ]; then
fi
```

### `codegraph-vba dead-code`

Lists the symbols nothing in the index reaches, after every reason to doubt the claim: test code, generated and vendored files, decorated or overriding members, names the resolver failed to follow or that another symbol shares, and names written a second time in a file that can reach them (comments included). Read-only; it exits 0.

On VBA / Access it also leaves out what Access runs without a call in the source:

- event procedures: wired to a form, report or control, or named `<Control>_<Event>`, `Form_<Event>` or `Report_<Event>` in a form or report module, plus `Class_Initialize`, `Class_Terminate` and `WithEvents` handlers;
- functions a form or report expression or SQL calls (`OnClick ="=F()"`, `ControlSource`, `RecordSource`, `RowSource`);
- `Implements` members (`<Interface>_<Member>`) and the interface class's own members;
- Dysflow test-harness modules (`Test_*.bas`, `TestHelper.bas`), counted as test code;
- modules, classes, labels, parameters and every other non-procedure node.

On VBA the "written a second time" check reads every VBA file and ignores case, comments (including a comment continued with ` _` and a `: Rem` remark) and the procedure's own body, where a Function assigns its return value through its own name; a string such as `Application.Run "Name"` still counts as a use. The code a `.form.txt` / `.report.txt` carries after `CodeBehindForm` is read only when the form has no `.cls` of its own. If a VBA file cannot be read, the procedures declared in it are left out (counted as unreadable), every other row stays listed, and the report prints a `Warning:` line naming the file and the reason (`unreadableVbaFiles` in `--json`), because a call in that file would not be seen.

A `Public` procedure in a standard `.bas` module that nothing calls is listed apart, under **Public, possibly external**: a macro, another database or `Application.Run` may call it. That includes a procedure with no access modifier, which VBA makes Public. The last line counts every exclusion. `--limit` takes a whole number of 1 or more (default 200) and applies to each list; anything else exits 1.

```bash
codegraph-vba dead-code # Current project
codegraph-vba dead-code -p ../front # Another project
codegraph-vba dead-code --json # entries, publicPossiblyExternal, excluded, vbaExcluded, unreadableVbaFiles
```

---

## MCP Tools
Expand Down
26 changes: 26 additions & 0 deletions __tests__/fixtures/vba-dead-code/Circle.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Circle"
Option Compare Database
Option Explicit

Implements IShape

Private WithEvents m_btn As CommandButton

' Class event: VBA calls it when the object is created.
Private Sub Class_Initialize()
Set m_btn = Nothing
End Sub

' Implements member: reached through IShape.Draw, never by its own name.
Private Sub IShape_Draw()
Debug.Print "circle"
End Sub

' WithEvents handler for a control class outside the project.
Private Sub m_btn_Click()
Debug.Print "click"
End Sub
24 changes: 24 additions & 0 deletions __tests__/fixtures/vba-dead-code/Form_Orders.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Form_Orders"
Option Compare Database
Option Explicit

' Form-level event: wired to the form-layout node.
Private Sub Form_Load()
Dim book As Ledger
Set book = New Ledger
Me.cmdSave.Caption = "Save"
End Sub

' Control event: wired to the cmdSave control.
Private Sub cmdSave_Click()
Me.cmdSave.Caption = "Saved"
End Sub

' Control event procedure for a control the layout does not declare.
Private Sub txtGhost_AfterUpdate()
Debug.Print "ghost"
End Sub
34 changes: 34 additions & 0 deletions __tests__/fixtures/vba-dead-code/Form_Orders.form.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Version =21
VersionRequired =20
Checksum =-123456789
Begin Form
Caption ="Orders"
RecordSource ="SELECT Id, CustomerLabel(Id) AS Label FROM tblOrders"
Width =7000
Height =4000
Begin CommandButton
Name ="cmdSave"
Caption ="Save"
Left =500
Top =500
Width =1500
Height =400
End
Begin CommandButton
Name ="cmdOpen"
Caption ="Open"
OnClick ="=OpenOrders()"
Left =2500
Top =500
Width =1500
Height =400
End
Begin TextBox
Name ="txtTotal"
ControlSource ="=CalcTotal([Id])"
Left =500
Top =1200
Width =1500
Height =300
End
End
31 changes: 31 additions & 0 deletions __tests__/fixtures/vba-dead-code/Form_Orphan.form.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Version =21
VersionRequired =20
Checksum =-123456789
Begin Form
Caption ="Orphan"
Width =7000
Height =4000
Begin CommandButton
Name ="cmdRun"
Caption ="Run"
OnClick ="[Event Procedure]"
Left =500
Top =500
Width =1500
Height =400
End
End
CodeBehindForm
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Attribute VB_Name = "Form_Orphan"
Option Compare Database
Option Explicit

' This form was exported without its Form_Orphan.cls: the code below is the
' only copy of the module, so it is read as mention text.
Private Sub cmdRun_Click()
OrphanTarget
End Sub
12 changes: 12 additions & 0 deletions __tests__/fixtures/vba-dead-code/IShape.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "IShape"
Option Compare Database
Option Explicit

' Interface class: Circle implements it.

Public Sub Draw()
End Sub
13 changes: 13 additions & 0 deletions __tests__/fixtures/vba-dead-code/Ledger.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Ledger"
Option Compare Database
Option Explicit

' Read late-bound from ModMain, which declares no Ledger: no edge, and ModMain
' is not one of this file's dependents. Still a use.
Public Property Get LateBalance() As Double
LateBalance = 1
End Property
84 changes: 84 additions & 0 deletions __tests__/fixtures/vba-dead-code/ModMain.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
Attribute VB_Name = "ModMain"
Option Compare Database
Option Explicit

' Issue #344 fixture: the standard module. See __tests__/vba-dead-code.test.ts
' for what each procedure covers.

Public Sub Entry()
Dim shape As IShape
Set shape = New Circle
shape.Draw
UsedHelper
Dim anything As Object
Debug.Print anything.LateBalance
End Sub

Private Sub UsedHelper()
Debug.Print "used"
End Sub

Private Sub TrulyDead(ByVal count As Long)
On Error GoTo Fail
Debug.Print count
Exit Sub
Fail:
Debug.Print "fail"
End Sub

Public Function CalcTotal(ByVal id As Long) As Double
CalcTotal = id * 2
End Function

Public Function CustomerLabel(ByVal id As Long) As String
CustomerLabel = "C" & id
End Function

Public Function OpenOrders() As Boolean
OpenOrders = True
End Function

' A dead Function that assigns its own return value and names itself in its
' error message: only its own body writes the name, so it is still dead.
Private Function SelfNamed() As String
On Error GoTo Fail
SelfNamed = "x"
Exit Function
Fail:
SelfNamed = "El metodo SelfNamed ha fallado"
End Function

' Only a comment names this one elsewhere: a comment is not a call.
' SelfCommented
Private Sub SelfCommented()
Debug.Print "comment"
End Sub

' Called by Application.Run from a string, which the mention rule still honours.
Private Sub RunByName()
Debug.Print "run"
End Sub

Public Sub Runner()
Application.Run "RunByName"
End Sub

' No access modifier: in a standard module that is Public by default.
Sub Implicita()
Debug.Print "implicit"
End Sub

' A comment that continues onto the next line with " _" is still a comment, _
so this line naming ContinuedTarget is not a call.
Private Sub ContinuedTarget()
Debug.Print "continued": Rem RemTarget is named only in this Rem comment
End Sub

Private Sub RemTarget()
Debug.Print "rem"
End Sub

' Called only from the code of Form_Orphan.form.txt, which has no .cls of its own.
Public Sub OrphanTarget()
Debug.Print "orphan"
End Sub
17 changes: 17 additions & 0 deletions __tests__/fixtures/vba-dead-code/Test_ModMain.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Attribute VB_Name = "Test_ModMain"
Option Compare Database
Option Explicit

' Dysflow test-harness module: everything here is test code.

Public Sub Test_Entry()
ArrangeHelper
End Sub

Private Sub ArrangeHelper()
Debug.Print "arrange"
End Sub

Private Sub UnusedTestHelper()
Debug.Print "unused"
End Sub
Loading