Skip to content

About

The static code analysis engine for Go

Resources

Contributing

Stars

0 stars

Watchers

2 watching

Forks

Repository files navigation

CodeLLM-DevKit

codeanalyzer-go (cango)

The Go backend of CLDK: a symbol table, call graph and dependence graphs for Go projects, as analysis.json or a Neo4j graph.

PyPI Go Release License


cango builds the symbol table, call graph, and control-flow and data-flow graphs of a Go module, with full type information from go/types. It writes them as JSON, in the same shape as the Python, TypeScript and Java analyzers, or as a Neo4j graph.

Install

# Shell installer (macOS, Linux)
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/codellm-devkit/codeanalyzer-go/releases/latest/download/cango-installer.sh | sh

# Homebrew
brew install codellm-devkit/homebrew-tap/codeanalyzer-go

# PyPI (the package the CLDK Python SDK uses)
pip install codeanalyzer-go

From source (Go 1.26 or later):

go build -o cango ./cmd/codeanalyzer

Note

cango type-checks with the go/types built into it. Build it with a Go at least as new as the newest Go that your projects need. The release binaries use the latest Go. A build with an older Go warns and leaves some callees unresolved.

Neo4j

Start Neo4j in Docker:

docker run -d --name neo4j -p 7474:7474 -p 7687:7687 \
  -e NEO4J_AUTH=neo4j/your-password neo4j:5.26-community

Push the analysis of a project into it:

cango -i ./my-project --emit neo4j \
  --neo4j-uri bolt://127.0.0.1:7687 --neo4j-password your-password

Then open http://localhost:7474 and run a query, for example MATCH (c:GoCallable) RETURN c.signature LIMIT 25.

Without --neo4j-uri, cango writes a Cypher snapshot that you can load later:

cango -i ./my-project --emit neo4j -o ./out    # writes ./out/graph.cypher
docker exec -i neo4j cypher-shell -u neo4j -p your-password < ./out/graph.cypher

--emit neo4j always projects level 4, with the same ids as the JSON output. A snapshot replaces the app's graph when loaded. A Bolt push only adds and updates. Pass --eager to rebuild the app's graph and drop stale nodes. Every write stays inside the app's can:// prefix, so other apps in the same database keep their graphs.

Labels start with Go (:GoType, :GoCallable, …) and relationships with GO_ (GO_CALLS, GO_DDG, …). See neo4j-projection.md. scripts/load_and_query.sh loads a snapshot and runs sample queries.

Usage

Point -i at the directory that holds the project's go.mod:

cango -i ./my-project                  # symbol table, to stdout
cango -i ./my-project -a 2 -o ./out    # + call graph, to ./out/analysis.json
cango -i ./my-project -a 4 -o ./out    # + intra- and interprocedural dataflow
cango -i ./my-project -t pkg/a.go      # only these files (incremental; not at -a 4)
$ cango --help
Usage:
  cango [flags]

Aliases:
  cango, codeanalyzer-go

Flags:
  -a, --analysis-level int      Analysis level: 1=symbol table, 2=+call graph, 3=+intraprocedural cfg/cdg/ddg, 4=+interprocedural SDG (default 1)
      --app-name string         Application anchor name for can:// ids and Neo4j :Application (default: input dir name)
      --artifact-text           Capture each text artifact's whole source in the output (default true)
  -c, --cache-dir string        Cache directory (default: ~/.cldk/go-cache)
      --eager                   Force clean rebuild (ignore cache)
      --emit string             Output projection: json|neo4j|schema (default "json")
  -f, --format string           Output format: json|msgpack (default "json")
      --graph-field-depth int   Field-path depth bound for L3 dataflow nodes (k limit; applies at -a 3 and -a 4) (default 3)
  -h, --help                    help for cango
  -i, --input string            Project root to analyze (required)
  -j, --jobs int                Worker parallelism (default: CPU cores)
      --library-models string   JSON file of library stand-in flows overriding the all-to-all default (-a 4 and --emit neo4j only)
      --namespace string        Identity namespace (default: application name)
      --neo4j-database string   Neo4j database (env NEO4J_DATABASE, optional)
      --neo4j-password string   Neo4j password (env NEO4J_PASSWORD, default neo4j)
      --neo4j-uri string        Live Bolt push target (env NEO4J_URI); omit to write graph.cypher
      --neo4j-user string       Neo4j username (env NEO4J_USERNAME, default neo4j)
      --no-artifact-text        Keep the artifact inventory but drop the captured text
  -o, --output string           Output directory for analysis.json (default: stdout)
      --skip-tests              Skip *_test.go files (default true)
      --strict                  Fail non-zero on any L4 degradation (default: degrade and record it)
  -t, --target-files strings    Restrict analysis to specific files (incremental mode)
  -v, --verbose count           Verbosity (repeat for more detail)
      --version                 Print version and exit
Level Adds
-a 1 (default) Symbol table: modules, types, callables, fields, call sites, repository artifacts and go.mod dependencies
-a 2 Call graph, including calls into libraries (application.external_symbols)
-a 3 Per-callable statement nodes and cfg, cdg, ddg
-a 4 System dependence graph: VTA dispatch, parameter vertices, param_in / param_out, summary edges, library stand-ins

Every node has a can://<namespace>/<app>/… id. --app-name (default: the input directory's name) and --namespace (default: the app name) set the two segments. If level 4 cannot build everything, it still writes its output and lists what it skipped in degraded[]. --strict makes that a non-zero exit.

Output

cango -i testdata/greeter -a 2, trimmed (… marks what the example leaves out):

{
  "schema_version": "2.0.0",
  "identity_version": 2,
  "language": "go",
  "max_level": 2,
  "analyzer": { "name": "codeanalyzer-go", "version": "0.4.0" },
  "application": {
    "id": "can://greeter/greeter",
    "kind": "application",
    "name": "greeter",
    "symbol_table": {
      "pkg/greeter/greeter.go": {
        "id": "can://greeter/greeter/go/pkg/greeter/greeter.go",
        "kind": "module",
        "package": "greeter",
        "imports": [{ "name": "fmt", "path": "fmt", … }],
        "types": {
          "Greeter": {
            "id": "can://greeter/greeter/go/pkg/greeter/greeter.go/Greeter",
            "kind": "struct",
            "signature": "example.com/greeter/pkg/greeter.Greeter",
            "fields": { "Prefix": { "kind": "field", "type": "string", … } },
            "callables": {
              "example.com/greeter/pkg/greeter.Greeter.Greet": {
                "id": "can://greeter/greeter/go/pkg/greeter/greeter.go/Greeter/Greet",
                "kind": "method",
                "parameters": [{ "name": "name", "type": "string", … }],
                "return_type": "string",
                "body": {
                  "18:9": {
                    "kind": "call",
                    "id": "can://greeter/greeter/go/pkg/greeter/greeter.go/Greeter/Greet@18:9",
                    "callee": "can://greeter/greeter/@external/fmt/Sprintf",
                    "span": { "start": [18, 9], "end": [18, 47], "bytes": [393, 431] },
                    "start_line": 18, "end_line": 18
                  }
                },
                …
              }
            }
          }
        },
        "functions": { "example.com/greeter/pkg/greeter.New": { … } },
        "source": "// Package greeter …"
      }
    },
    "call_graph": [
      {
        "src": "can://greeter/greeter/go/pkg/greeter/greeter.go/Greeter/Greet",
        "dst": "can://greeter/greeter/@external/fmt/Sprintf",
        "prov": ["go/types"], "weight": 1
      },
      …
    ],
    "external_symbols": { "can://greeter/greeter/@external/fmt/Sprintf": { "kind": "external", … } },
    "artifacts": { "go.mod": { "id": "can://greeter/greeter/artifact/go.mod", "format": "gomod", … } }
  }
}

Levels 3 and 4 add cfg, cdg, ddg and summary to each callable, and param_in / param_out to the application.

Python SDK

from cldk import CLDK

analysis = CLDK(language="go").analysis(project_path="/path/to/go/project")

Go support needs cldk 2.1.0 or later. Levels 3 and 4 need cldk 2.2.0rc1 or later. The SDK finds codeanalyzer-go on PATH. See python-sdk.

Development

go test ./...

The fixtures under testdata/ include their dependencies, so tests need no network. To release, push a vX.Y.Z tag: the release workflow publishes the binaries, the PyPI wheels and the Homebrew formula. CHANGELOG.md lists the changes.

License

Apache 2.0. See LICENSE.

About

The static code analysis engine for Go

Resources

Contributing

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages