@optave/codegraph 3.4.0 → 3.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -7
- package/dist/ast-analysis/engine.d.ts.map +1 -1
- package/dist/ast-analysis/engine.js +3 -9
- package/dist/ast-analysis/engine.js.map +1 -1
- package/dist/ast-analysis/shared.d.ts.map +1 -1
- package/dist/ast-analysis/shared.js +0 -1
- package/dist/ast-analysis/shared.js.map +1 -1
- package/dist/ast-analysis/visitors/cfg-conditionals.d.ts +5 -0
- package/dist/ast-analysis/visitors/cfg-conditionals.d.ts.map +1 -0
- package/dist/ast-analysis/visitors/cfg-conditionals.js +166 -0
- package/dist/ast-analysis/visitors/cfg-conditionals.js.map +1 -0
- package/dist/ast-analysis/visitors/cfg-loops.d.ts +7 -0
- package/dist/ast-analysis/visitors/cfg-loops.d.ts.map +1 -0
- package/dist/ast-analysis/visitors/cfg-loops.js +73 -0
- package/dist/ast-analysis/visitors/cfg-loops.js.map +1 -0
- package/dist/ast-analysis/visitors/cfg-shared.d.ts +56 -0
- package/dist/ast-analysis/visitors/cfg-shared.d.ts.map +1 -0
- package/dist/ast-analysis/visitors/cfg-shared.js +107 -0
- package/dist/ast-analysis/visitors/cfg-shared.js.map +1 -0
- package/dist/ast-analysis/visitors/cfg-try-catch.d.ts +4 -0
- package/dist/ast-analysis/visitors/cfg-try-catch.d.ts.map +1 -0
- package/dist/ast-analysis/visitors/cfg-try-catch.js +100 -0
- package/dist/ast-analysis/visitors/cfg-try-catch.js.map +1 -0
- package/dist/ast-analysis/visitors/cfg-visitor.d.ts +2 -2
- package/dist/ast-analysis/visitors/cfg-visitor.d.ts.map +1 -1
- package/dist/ast-analysis/visitors/cfg-visitor.js +11 -445
- package/dist/ast-analysis/visitors/cfg-visitor.js.map +1 -1
- package/dist/ast-analysis/visitors/complexity-visitor.d.ts.map +1 -1
- package/dist/ast-analysis/visitors/complexity-visitor.js.map +1 -1
- package/dist/ast-analysis/visitors/dataflow-visitor.d.ts.map +1 -1
- package/dist/ast-analysis/visitors/dataflow-visitor.js.map +1 -1
- package/dist/cli/commands/batch.d.ts.map +1 -1
- package/dist/cli/commands/batch.js +4 -3
- package/dist/cli/commands/batch.js.map +1 -1
- package/dist/cli/commands/branch-compare.js +1 -1
- package/dist/cli/commands/branch-compare.js.map +1 -1
- package/dist/cli/commands/build.js +1 -1
- package/dist/cli/commands/build.js.map +1 -1
- package/dist/cli/commands/info.d.ts.map +1 -1
- package/dist/cli/commands/info.js +1 -2
- package/dist/cli/commands/info.js.map +1 -1
- package/dist/cli/commands/path.d.ts.map +1 -1
- package/dist/cli/commands/path.js +7 -2
- package/dist/cli/commands/path.js.map +1 -1
- package/dist/cli/commands/plot.d.ts.map +1 -1
- package/dist/cli/commands/plot.js +2 -2
- package/dist/cli/commands/plot.js.map +1 -1
- package/dist/cli/commands/watch.js +1 -1
- package/dist/cli/commands/watch.js.map +1 -1
- package/dist/cli/index.js +2 -2
- package/dist/cli/index.js.map +1 -1
- package/dist/cli/shared/open-graph.d.ts +2 -2
- package/dist/cli/shared/open-graph.d.ts.map +1 -1
- package/dist/cli/shared/open-graph.js.map +1 -1
- package/dist/cli/types.d.ts +1 -1
- package/dist/cli/types.d.ts.map +1 -1
- package/dist/cli.js +2 -3
- package/dist/cli.js.map +1 -1
- package/dist/db/connection.d.ts +17 -0
- package/dist/db/connection.d.ts.map +1 -1
- package/dist/db/connection.js +91 -2
- package/dist/db/connection.js.map +1 -1
- package/dist/db/index.d.ts +1 -1
- package/dist/db/index.d.ts.map +1 -1
- package/dist/db/index.js +1 -1
- package/dist/db/index.js.map +1 -1
- package/dist/db/migrations.d.ts.map +1 -1
- package/dist/db/migrations.js +7 -0
- package/dist/db/migrations.js.map +1 -1
- package/dist/domain/analysis/brief.d.ts.map +1 -1
- package/dist/domain/analysis/brief.js +1 -3
- package/dist/domain/analysis/brief.js.map +1 -1
- package/dist/domain/analysis/context.d.ts.map +1 -1
- package/dist/domain/analysis/context.js +2 -4
- package/dist/domain/analysis/context.js.map +1 -1
- package/dist/domain/analysis/dependencies.d.ts +49 -0
- package/dist/domain/analysis/dependencies.d.ts.map +1 -1
- package/dist/domain/analysis/dependencies.js +145 -0
- package/dist/domain/analysis/dependencies.js.map +1 -1
- package/dist/domain/analysis/diff-impact.d.ts +76 -0
- package/dist/domain/analysis/diff-impact.d.ts.map +1 -0
- package/dist/domain/analysis/diff-impact.js +282 -0
- package/dist/domain/analysis/diff-impact.js.map +1 -0
- package/dist/domain/analysis/exports.d.ts.map +1 -1
- package/dist/domain/analysis/exports.js +0 -1
- package/dist/domain/analysis/exports.js.map +1 -1
- package/dist/domain/analysis/fn-impact.d.ts +66 -0
- package/dist/domain/analysis/fn-impact.d.ts.map +1 -0
- package/dist/domain/analysis/fn-impact.js +189 -0
- package/dist/domain/analysis/fn-impact.js.map +1 -0
- package/dist/domain/analysis/impact.d.ts +8 -148
- package/dist/domain/analysis/impact.d.ts.map +1 -1
- package/dist/domain/analysis/impact.js +8 -568
- package/dist/domain/analysis/impact.js.map +1 -1
- package/dist/domain/analysis/module-map.d.ts.map +1 -1
- package/dist/domain/analysis/module-map.js +1 -3
- package/dist/domain/analysis/module-map.js.map +1 -1
- package/dist/domain/graph/builder/context.d.ts +2 -3
- package/dist/domain/graph/builder/context.d.ts.map +1 -1
- package/dist/domain/graph/builder/context.js.map +1 -1
- package/dist/domain/graph/builder/helpers.d.ts +4 -5
- package/dist/domain/graph/builder/helpers.d.ts.map +1 -1
- package/dist/domain/graph/builder/helpers.js +1 -2
- package/dist/domain/graph/builder/helpers.js.map +1 -1
- package/dist/domain/graph/builder/incremental.d.ts +2 -3
- package/dist/domain/graph/builder/incremental.d.ts.map +1 -1
- package/dist/domain/graph/builder/incremental.js.map +1 -1
- package/dist/domain/graph/builder/pipeline.d.ts.map +1 -1
- package/dist/domain/graph/builder/pipeline.js +6 -0
- package/dist/domain/graph/builder/pipeline.js.map +1 -1
- package/dist/domain/graph/builder/stages/build-edges.d.ts.map +1 -1
- package/dist/domain/graph/builder/stages/build-edges.js +12 -2
- package/dist/domain/graph/builder/stages/build-edges.js.map +1 -1
- package/dist/domain/graph/builder/stages/build-structure.d.ts.map +1 -1
- package/dist/domain/graph/builder/stages/build-structure.js +155 -59
- package/dist/domain/graph/builder/stages/build-structure.js.map +1 -1
- package/dist/domain/graph/builder/stages/detect-changes.d.ts.map +1 -1
- package/dist/domain/graph/builder/stages/detect-changes.js +6 -6
- package/dist/domain/graph/builder/stages/detect-changes.js.map +1 -1
- package/dist/domain/graph/builder/stages/finalize.d.ts.map +1 -1
- package/dist/domain/graph/builder/stages/finalize.js +85 -61
- package/dist/domain/graph/builder/stages/finalize.js.map +1 -1
- package/dist/domain/graph/builder/stages/insert-nodes.d.ts.map +1 -1
- package/dist/domain/graph/builder/stages/insert-nodes.js.map +1 -1
- package/dist/domain/graph/builder/stages/resolve-imports.d.ts.map +1 -1
- package/dist/domain/graph/builder/stages/resolve-imports.js +58 -11
- package/dist/domain/graph/builder/stages/resolve-imports.js.map +1 -1
- package/dist/domain/graph/cycles.js +2 -2
- package/dist/domain/graph/cycles.js.map +1 -1
- package/dist/domain/graph/resolve.d.ts.map +1 -1
- package/dist/domain/graph/resolve.js +10 -8
- package/dist/domain/graph/resolve.js.map +1 -1
- package/dist/domain/graph/watcher.d.ts.map +1 -1
- package/dist/domain/graph/watcher.js +1 -3
- package/dist/domain/graph/watcher.js.map +1 -1
- package/dist/domain/parser.d.ts.map +1 -1
- package/dist/domain/parser.js +11 -12
- package/dist/domain/parser.js.map +1 -1
- package/dist/domain/queries.d.ts +3 -2
- package/dist/domain/queries.d.ts.map +1 -1
- package/dist/domain/queries.js +3 -2
- package/dist/domain/queries.js.map +1 -1
- package/dist/domain/search/generator.d.ts.map +1 -1
- package/dist/domain/search/generator.js.map +1 -1
- package/dist/extractors/csharp.js +2 -2
- package/dist/extractors/csharp.js.map +1 -1
- package/dist/extractors/go.js +2 -2
- package/dist/extractors/go.js.map +1 -1
- package/dist/extractors/helpers.d.ts +5 -0
- package/dist/extractors/helpers.d.ts.map +1 -1
- package/dist/extractors/helpers.js +5 -0
- package/dist/extractors/helpers.js.map +1 -1
- package/dist/extractors/javascript.js +58 -60
- package/dist/extractors/javascript.js.map +1 -1
- package/dist/extractors/php.js +2 -2
- package/dist/extractors/php.js.map +1 -1
- package/dist/extractors/python.js +2 -2
- package/dist/extractors/python.js.map +1 -1
- package/dist/extractors/rust.js +2 -2
- package/dist/extractors/rust.js.map +1 -1
- package/dist/features/audit.d.ts.map +1 -1
- package/dist/features/audit.js +1 -2
- package/dist/features/audit.js.map +1 -1
- package/dist/features/branch-compare.d.ts.map +1 -1
- package/dist/features/branch-compare.js +2 -3
- package/dist/features/branch-compare.js.map +1 -1
- package/dist/features/cfg.d.ts.map +1 -1
- package/dist/features/cfg.js +2 -4
- package/dist/features/cfg.js.map +1 -1
- package/dist/features/cochange.js +4 -4
- package/dist/features/cochange.js.map +1 -1
- package/dist/features/communities.js +4 -4
- package/dist/features/communities.js.map +1 -1
- package/dist/features/complexity-query.d.ts +37 -0
- package/dist/features/complexity-query.d.ts.map +1 -0
- package/dist/features/complexity-query.js +263 -0
- package/dist/features/complexity-query.js.map +1 -0
- package/dist/features/complexity.d.ts +2 -30
- package/dist/features/complexity.d.ts.map +1 -1
- package/dist/features/complexity.js +7 -261
- package/dist/features/complexity.js.map +1 -1
- package/dist/features/dataflow.d.ts.map +1 -1
- package/dist/features/dataflow.js +8 -24
- package/dist/features/dataflow.js.map +1 -1
- package/dist/features/export.d.ts +7 -8
- package/dist/features/export.d.ts.map +1 -1
- package/dist/features/export.js.map +1 -1
- package/dist/features/flow.d.ts.map +1 -1
- package/dist/features/flow.js.map +1 -1
- package/dist/features/graph-enrichment.d.ts.map +1 -1
- package/dist/features/graph-enrichment.js +1 -3
- package/dist/features/graph-enrichment.js.map +1 -1
- package/dist/features/manifesto.js +8 -8
- package/dist/features/manifesto.js.map +1 -1
- package/dist/features/snapshot.d.ts.map +1 -1
- package/dist/features/snapshot.js +0 -1
- package/dist/features/snapshot.js.map +1 -1
- package/dist/features/structure-query.d.ts +76 -0
- package/dist/features/structure-query.d.ts.map +1 -0
- package/dist/features/structure-query.js +245 -0
- package/dist/features/structure-query.js.map +1 -0
- package/dist/features/structure.d.ts +12 -67
- package/dist/features/structure.d.ts.map +1 -1
- package/dist/features/structure.js +188 -244
- package/dist/features/structure.js.map +1 -1
- package/dist/features/triage.js +2 -2
- package/dist/features/triage.js.map +1 -1
- package/dist/graph/algorithms/leiden/adapter.d.ts.map +1 -1
- package/dist/graph/algorithms/leiden/adapter.js +2 -9
- package/dist/graph/algorithms/leiden/adapter.js.map +1 -1
- package/dist/graph/classifiers/roles.d.ts +5 -1
- package/dist/graph/classifiers/roles.d.ts.map +1 -1
- package/dist/graph/classifiers/roles.js +20 -12
- package/dist/graph/classifiers/roles.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/infrastructure/config.d.ts.map +1 -1
- package/dist/infrastructure/config.js +12 -11
- package/dist/infrastructure/config.js.map +1 -1
- package/dist/infrastructure/native.d.ts.map +1 -1
- package/dist/infrastructure/native.js +7 -3
- package/dist/infrastructure/native.js.map +1 -1
- package/dist/infrastructure/registry.d.ts.map +1 -1
- package/dist/infrastructure/registry.js +1 -1
- package/dist/infrastructure/registry.js.map +1 -1
- package/dist/infrastructure/update-check.js +3 -3
- package/dist/infrastructure/update-check.js.map +1 -1
- package/dist/mcp/server.d.ts.map +1 -1
- package/dist/mcp/server.js +2 -8
- package/dist/mcp/server.js.map +1 -1
- package/dist/mcp/tool-registry.d.ts.map +1 -1
- package/dist/mcp/tool-registry.js +9 -4
- package/dist/mcp/tool-registry.js.map +1 -1
- package/dist/mcp/tools/audit.js +1 -1
- package/dist/mcp/tools/audit.js.map +1 -1
- package/dist/mcp/tools/cfg.js +1 -1
- package/dist/mcp/tools/cfg.js.map +1 -1
- package/dist/mcp/tools/check.js +2 -2
- package/dist/mcp/tools/check.js.map +1 -1
- package/dist/mcp/tools/dataflow.js +2 -2
- package/dist/mcp/tools/dataflow.js.map +1 -1
- package/dist/mcp/tools/export-graph.js +1 -1
- package/dist/mcp/tools/export-graph.js.map +1 -1
- package/dist/mcp/tools/index.d.ts.map +1 -1
- package/dist/mcp/tools/index.js.map +1 -1
- package/dist/mcp/tools/path.d.ts +1 -0
- package/dist/mcp/tools/path.d.ts.map +1 -1
- package/dist/mcp/tools/path.js +9 -0
- package/dist/mcp/tools/path.js.map +1 -1
- package/dist/mcp/tools/query.js +1 -1
- package/dist/mcp/tools/query.js.map +1 -1
- package/dist/mcp/tools/semantic-search.js +1 -1
- package/dist/mcp/tools/semantic-search.js.map +1 -1
- package/dist/mcp/tools/sequence.js +1 -1
- package/dist/mcp/tools/sequence.js.map +1 -1
- package/dist/mcp/tools/symbol-children.js +1 -1
- package/dist/mcp/tools/symbol-children.js.map +1 -1
- package/dist/mcp/tools/triage.js +1 -1
- package/dist/mcp/tools/triage.js.map +1 -1
- package/dist/presentation/audit.d.ts.map +1 -1
- package/dist/presentation/audit.js +0 -1
- package/dist/presentation/audit.js.map +1 -1
- package/dist/presentation/diff-impact-mermaid.d.ts +11 -0
- package/dist/presentation/diff-impact-mermaid.d.ts.map +1 -0
- package/dist/presentation/diff-impact-mermaid.js +105 -0
- package/dist/presentation/diff-impact-mermaid.js.map +1 -0
- package/dist/presentation/flow.d.ts.map +1 -1
- package/dist/presentation/flow.js +0 -2
- package/dist/presentation/flow.js.map +1 -1
- package/dist/presentation/manifesto.d.ts.map +1 -1
- package/dist/presentation/manifesto.js +0 -1
- package/dist/presentation/manifesto.js.map +1 -1
- package/dist/presentation/queries-cli/inspect.d.ts.map +1 -1
- package/dist/presentation/queries-cli/inspect.js.map +1 -1
- package/dist/presentation/queries-cli/path.d.ts.map +1 -1
- package/dist/presentation/queries-cli/path.js +45 -1
- package/dist/presentation/queries-cli/path.js.map +1 -1
- package/dist/presentation/result-formatter.d.ts.map +1 -1
- package/dist/presentation/result-formatter.js +1 -3
- package/dist/presentation/result-formatter.js.map +1 -1
- package/dist/presentation/sequence.d.ts.map +1 -1
- package/dist/presentation/sequence.js +0 -1
- package/dist/presentation/sequence.js.map +1 -1
- package/dist/presentation/structure.d.ts.map +1 -1
- package/dist/presentation/structure.js.map +1 -1
- package/dist/presentation/triage.d.ts.map +1 -1
- package/dist/presentation/triage.js +0 -1
- package/dist/presentation/triage.js.map +1 -1
- package/dist/shared/constants.d.ts +9 -3
- package/dist/shared/constants.d.ts.map +1 -1
- package/dist/shared/constants.js +6 -3
- package/dist/shared/constants.js.map +1 -1
- package/dist/shared/errors.d.ts +2 -0
- package/dist/shared/errors.d.ts.map +1 -1
- package/dist/shared/errors.js +4 -0
- package/dist/shared/errors.js.map +1 -1
- package/dist/shared/version.d.ts +2 -0
- package/dist/shared/version.d.ts.map +1 -0
- package/dist/shared/version.js +5 -0
- package/dist/shared/version.js.map +1 -0
- package/dist/types.d.ts +2 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +8 -7
- package/src/ast-analysis/engine.ts +3 -9
- package/src/ast-analysis/shared.ts +0 -1
- package/src/ast-analysis/visitors/cfg-conditionals.ts +227 -0
- package/src/ast-analysis/visitors/cfg-loops.ts +136 -0
- package/src/ast-analysis/visitors/cfg-shared.ts +196 -0
- package/src/ast-analysis/visitors/cfg-try-catch.ts +142 -0
- package/src/ast-analysis/visitors/cfg-visitor.ts +34 -655
- package/src/ast-analysis/visitors/complexity-visitor.ts +0 -1
- package/src/ast-analysis/visitors/dataflow-visitor.ts +0 -1
- package/src/cli/commands/batch.ts +4 -3
- package/src/cli/commands/branch-compare.ts +1 -1
- package/src/cli/commands/build.ts +1 -1
- package/src/cli/commands/info.ts +1 -2
- package/src/cli/commands/path.ts +7 -2
- package/src/cli/commands/plot.ts +2 -2
- package/src/cli/commands/watch.ts +1 -1
- package/src/cli/index.ts +2 -2
- package/src/cli/shared/open-graph.ts +2 -2
- package/src/cli/types.ts +1 -1
- package/src/cli.ts +2 -3
- package/src/db/connection.ts +97 -13
- package/src/db/index.ts +2 -0
- package/src/db/migrations.ts +7 -0
- package/src/domain/analysis/brief.ts +0 -1
- package/src/domain/analysis/context.ts +2 -6
- package/src/domain/analysis/dependencies.ts +165 -0
- package/src/domain/analysis/diff-impact.ts +354 -0
- package/src/domain/analysis/exports.ts +0 -2
- package/src/domain/analysis/fn-impact.ts +241 -0
- package/src/domain/analysis/impact.ts +8 -718
- package/src/domain/analysis/module-map.ts +1 -5
- package/src/domain/graph/builder/context.ts +2 -2
- package/src/domain/graph/builder/helpers.ts +14 -11
- package/src/domain/graph/builder/incremental.ts +33 -28
- package/src/domain/graph/builder/pipeline.ts +8 -0
- package/src/domain/graph/builder/stages/build-edges.ts +17 -4
- package/src/domain/graph/builder/stages/build-structure.ts +205 -76
- package/src/domain/graph/builder/stages/detect-changes.ts +11 -12
- package/src/domain/graph/builder/stages/finalize.ts +100 -81
- package/src/domain/graph/builder/stages/insert-nodes.ts +12 -8
- package/src/domain/graph/builder/stages/resolve-imports.ts +75 -10
- package/src/domain/graph/cycles.ts +2 -2
- package/src/domain/graph/resolve.ts +14 -8
- package/src/domain/graph/watcher.ts +2 -4
- package/src/domain/parser.ts +11 -13
- package/src/domain/queries.ts +2 -2
- package/src/domain/search/generator.ts +3 -4
- package/src/extractors/csharp.ts +2 -2
- package/src/extractors/go.ts +2 -2
- package/src/extractors/helpers.ts +6 -0
- package/src/extractors/javascript.ts +58 -61
- package/src/extractors/php.ts +2 -2
- package/src/extractors/python.ts +2 -2
- package/src/extractors/rust.ts +2 -2
- package/src/features/audit.ts +1 -2
- package/src/features/branch-compare.ts +3 -9
- package/src/features/cfg.ts +2 -4
- package/src/features/cochange.ts +4 -4
- package/src/features/communities.ts +4 -4
- package/src/features/complexity-query.ts +370 -0
- package/src/features/complexity.ts +6 -365
- package/src/features/dataflow.ts +48 -70
- package/src/features/export.ts +12 -16
- package/src/features/flow.ts +0 -1
- package/src/features/graph-enrichment.ts +1 -3
- package/src/features/manifesto.ts +8 -8
- package/src/features/snapshot.ts +1 -2
- package/src/features/structure-query.ts +387 -0
- package/src/features/structure.ts +231 -376
- package/src/features/triage.ts +2 -2
- package/src/graph/algorithms/leiden/adapter.ts +2 -9
- package/src/graph/classifiers/roles.ts +22 -13
- package/src/index.ts +1 -0
- package/src/infrastructure/config.ts +12 -13
- package/src/infrastructure/native.ts +7 -3
- package/src/infrastructure/registry.ts +1 -1
- package/src/infrastructure/update-check.ts +3 -3
- package/src/mcp/server.ts +2 -10
- package/src/mcp/tool-registry.ts +11 -4
- package/src/mcp/tools/audit.ts +1 -1
- package/src/mcp/tools/cfg.ts +1 -1
- package/src/mcp/tools/check.ts +2 -2
- package/src/mcp/tools/dataflow.ts +2 -2
- package/src/mcp/tools/export-graph.ts +1 -1
- package/src/mcp/tools/index.ts +0 -1
- package/src/mcp/tools/path.ts +10 -0
- package/src/mcp/tools/query.ts +1 -1
- package/src/mcp/tools/semantic-search.ts +1 -1
- package/src/mcp/tools/sequence.ts +1 -1
- package/src/mcp/tools/symbol-children.ts +1 -1
- package/src/mcp/tools/triage.ts +1 -1
- package/src/presentation/audit.ts +0 -1
- package/src/presentation/diff-impact-mermaid.ts +127 -0
- package/src/presentation/flow.ts +0 -2
- package/src/presentation/manifesto.ts +0 -1
- package/src/presentation/queries-cli/inspect.ts +0 -1
- package/src/presentation/queries-cli/path.ts +71 -1
- package/src/presentation/result-formatter.ts +0 -1
- package/src/presentation/sequence.ts +0 -1
- package/src/presentation/structure.ts +0 -12
- package/src/presentation/triage.ts +0 -1
- package/src/shared/constants.ts +33 -19
- package/src/shared/errors.ts +5 -0
- package/src/shared/version.ts +10 -0
- package/src/types.ts +4 -10
- package/src/vendor.d.ts +0 -39
|
@@ -1,189 +1,31 @@
|
|
|
1
1
|
import type { TreeSitterNode, Visitor, VisitorContext } from '../../types.js';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
exitBlock: CfgBlockInternal | null;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
interface LoopCtx {
|
|
33
|
-
headerBlock: CfgBlockInternal;
|
|
34
|
-
exitBlock: CfgBlockInternal;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
interface FuncState {
|
|
38
|
-
blocks: CfgBlockInternal[];
|
|
39
|
-
edges: CfgEdgeInternal[];
|
|
40
|
-
makeBlock(
|
|
41
|
-
type: string,
|
|
42
|
-
startLine?: number | null,
|
|
43
|
-
endLine?: number | null,
|
|
44
|
-
label?: string | null,
|
|
45
|
-
): CfgBlockInternal;
|
|
46
|
-
addEdge(source: CfgBlockInternal, target: CfgBlockInternal, kind: string): void;
|
|
47
|
-
entryBlock: CfgBlockInternal;
|
|
48
|
-
exitBlock: CfgBlockInternal;
|
|
49
|
-
currentBlock: CfgBlockInternal | null;
|
|
50
|
-
loopStack: LoopCtx[];
|
|
51
|
-
labelMap: Map<string, LabelCtx>;
|
|
52
|
-
cfgStack: FuncState[];
|
|
53
|
-
funcNode: TreeSitterNode | null;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
interface CFGResultInternal {
|
|
57
|
-
funcNode: TreeSitterNode;
|
|
58
|
-
blocks: CfgBlockInternal[];
|
|
59
|
-
edges: CfgEdgeInternal[];
|
|
60
|
-
cyclomatic: number;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function isIfNode(type: string, cfgRules: AnyRules): boolean {
|
|
64
|
-
return type === cfgRules.ifNode || cfgRules.ifNodes?.has(type);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function isForNode(type: string, cfgRules: AnyRules): boolean {
|
|
68
|
-
return cfgRules.forNodes.has(type);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function isWhileNode(type: string, cfgRules: AnyRules): boolean {
|
|
72
|
-
return type === cfgRules.whileNode || cfgRules.whileNodes?.has(type);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function isSwitchNode(type: string, cfgRules: AnyRules): boolean {
|
|
76
|
-
return type === cfgRules.switchNode || cfgRules.switchNodes?.has(type);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function isCaseNode(type: string, cfgRules: AnyRules): boolean {
|
|
80
|
-
return (
|
|
81
|
-
type === cfgRules.caseNode || type === cfgRules.defaultNode || cfgRules.caseNodes?.has(type)
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function isBlockNode(type: string, cfgRules: AnyRules): boolean {
|
|
86
|
-
return type === 'statement_list' || type === cfgRules.blockNode || cfgRules.blockNodes?.has(type);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function isControlFlow(type: string, cfgRules: AnyRules): boolean {
|
|
90
|
-
return (
|
|
91
|
-
isIfNode(type, cfgRules) ||
|
|
92
|
-
(cfgRules.unlessNode && type === cfgRules.unlessNode) ||
|
|
93
|
-
isForNode(type, cfgRules) ||
|
|
94
|
-
isWhileNode(type, cfgRules) ||
|
|
95
|
-
(cfgRules.untilNode && type === cfgRules.untilNode) ||
|
|
96
|
-
(cfgRules.doNode && type === cfgRules.doNode) ||
|
|
97
|
-
(cfgRules.infiniteLoopNode && type === cfgRules.infiniteLoopNode) ||
|
|
98
|
-
isSwitchNode(type, cfgRules) ||
|
|
99
|
-
(cfgRules.tryNode && type === cfgRules.tryNode) ||
|
|
100
|
-
type === cfgRules.returnNode ||
|
|
101
|
-
type === cfgRules.throwNode ||
|
|
102
|
-
type === cfgRules.breakNode ||
|
|
103
|
-
type === cfgRules.continueNode ||
|
|
104
|
-
type === cfgRules.labeledNode
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function effectiveNode(node: TreeSitterNode, cfgRules: AnyRules): TreeSitterNode {
|
|
109
|
-
if (node.type === 'expression_statement' && node.namedChildCount === 1) {
|
|
110
|
-
const inner = nn(node.namedChild(0));
|
|
111
|
-
if (isControlFlow(inner.type, cfgRules)) return inner;
|
|
112
|
-
}
|
|
113
|
-
return node;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function registerLabelCtx(
|
|
117
|
-
S: FuncState,
|
|
118
|
-
headerBlock: CfgBlockInternal,
|
|
119
|
-
exitBlock: CfgBlockInternal,
|
|
120
|
-
): void {
|
|
121
|
-
for (const [, ctx] of Array.from(S.labelMap)) {
|
|
122
|
-
if (!ctx.headerBlock) {
|
|
123
|
-
ctx.headerBlock = headerBlock;
|
|
124
|
-
ctx.exitBlock = exitBlock;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function getBodyStatements(bodyNode: TreeSitterNode | null, cfgRules: AnyRules): TreeSitterNode[] {
|
|
130
|
-
if (!bodyNode) return [];
|
|
131
|
-
if (isBlockNode(bodyNode.type, cfgRules)) {
|
|
132
|
-
const stmts: TreeSitterNode[] = [];
|
|
133
|
-
for (let i = 0; i < bodyNode.namedChildCount; i++) {
|
|
134
|
-
const child = nn(bodyNode.namedChild(i));
|
|
135
|
-
if (child.type === 'statement_list') {
|
|
136
|
-
for (let j = 0; j < child.namedChildCount; j++) {
|
|
137
|
-
stmts.push(nn(child.namedChild(j)));
|
|
138
|
-
}
|
|
139
|
-
} else {
|
|
140
|
-
stmts.push(child);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
return stmts;
|
|
144
|
-
}
|
|
145
|
-
return [bodyNode];
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
function makeFuncState(): FuncState {
|
|
149
|
-
const blocks: CfgBlockInternal[] = [];
|
|
150
|
-
const edges: CfgEdgeInternal[] = [];
|
|
151
|
-
let nextIndex = 0;
|
|
152
|
-
|
|
153
|
-
function makeBlock(
|
|
154
|
-
type: string,
|
|
155
|
-
startLine: number | null = null,
|
|
156
|
-
endLine: number | null = null,
|
|
157
|
-
label: string | null = null,
|
|
158
|
-
): CfgBlockInternal {
|
|
159
|
-
const block: CfgBlockInternal = { index: nextIndex++, type, startLine, endLine, label };
|
|
160
|
-
blocks.push(block);
|
|
161
|
-
return block;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function addEdge(source: CfgBlockInternal, target: CfgBlockInternal, kind: string): void {
|
|
165
|
-
edges.push({ sourceIndex: source.index, targetIndex: target.index, kind });
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const entry = makeBlock('entry');
|
|
169
|
-
const exit = makeBlock('exit');
|
|
170
|
-
const firstBody = makeBlock('body');
|
|
171
|
-
addEdge(entry, firstBody, 'fallthrough');
|
|
172
|
-
|
|
173
|
-
return {
|
|
174
|
-
blocks,
|
|
175
|
-
edges,
|
|
176
|
-
makeBlock,
|
|
177
|
-
addEdge,
|
|
178
|
-
entryBlock: entry,
|
|
179
|
-
exitBlock: exit,
|
|
180
|
-
currentBlock: firstBody,
|
|
181
|
-
loopStack: [],
|
|
182
|
-
labelMap: new Map(),
|
|
183
|
-
cfgStack: [],
|
|
184
|
-
funcNode: null,
|
|
185
|
-
};
|
|
186
|
-
}
|
|
2
|
+
import { processIf, processSwitch } from './cfg-conditionals.js';
|
|
3
|
+
import {
|
|
4
|
+
processDoWhileLoop,
|
|
5
|
+
processForLoop,
|
|
6
|
+
processInfiniteLoop,
|
|
7
|
+
processWhileLoop,
|
|
8
|
+
} from './cfg-loops.js';
|
|
9
|
+
import type {
|
|
10
|
+
AnyRules,
|
|
11
|
+
CFGResultInternal,
|
|
12
|
+
CfgBlockInternal,
|
|
13
|
+
FuncState,
|
|
14
|
+
LabelCtx,
|
|
15
|
+
} from './cfg-shared.js';
|
|
16
|
+
import {
|
|
17
|
+
effectiveNode,
|
|
18
|
+
getBodyStatements,
|
|
19
|
+
isBlockNode,
|
|
20
|
+
isForNode,
|
|
21
|
+
isIfNode,
|
|
22
|
+
isSwitchNode,
|
|
23
|
+
isWhileNode,
|
|
24
|
+
makeFuncState,
|
|
25
|
+
} from './cfg-shared.js';
|
|
26
|
+
import { processTryCatch } from './cfg-try-catch.js';
|
|
27
|
+
|
|
28
|
+
export type { CfgBlockInternal } from './cfg-shared.js';
|
|
187
29
|
|
|
188
30
|
function processStatements(
|
|
189
31
|
stmts: TreeSitterNode[],
|
|
@@ -214,25 +56,25 @@ function processStatement(
|
|
|
214
56
|
return processLabeled(effNode, currentBlock, S, cfgRules);
|
|
215
57
|
}
|
|
216
58
|
if (isIfNode(type, cfgRules) || (cfgRules.unlessNode && type === cfgRules.unlessNode)) {
|
|
217
|
-
return processIf(effNode, currentBlock, S, cfgRules);
|
|
59
|
+
return processIf(effNode, currentBlock, S, cfgRules, processStatements);
|
|
218
60
|
}
|
|
219
61
|
if (isForNode(type, cfgRules)) {
|
|
220
|
-
return processForLoop(effNode, currentBlock, S, cfgRules);
|
|
62
|
+
return processForLoop(effNode, currentBlock, S, cfgRules, processStatements);
|
|
221
63
|
}
|
|
222
64
|
if (isWhileNode(type, cfgRules) || (cfgRules.untilNode && type === cfgRules.untilNode)) {
|
|
223
|
-
return processWhileLoop(effNode, currentBlock, S, cfgRules);
|
|
65
|
+
return processWhileLoop(effNode, currentBlock, S, cfgRules, processStatements);
|
|
224
66
|
}
|
|
225
67
|
if (cfgRules.doNode && type === cfgRules.doNode) {
|
|
226
|
-
return processDoWhileLoop(effNode, currentBlock, S, cfgRules);
|
|
68
|
+
return processDoWhileLoop(effNode, currentBlock, S, cfgRules, processStatements);
|
|
227
69
|
}
|
|
228
70
|
if (cfgRules.infiniteLoopNode && type === cfgRules.infiniteLoopNode) {
|
|
229
|
-
return processInfiniteLoop(effNode, currentBlock, S, cfgRules);
|
|
71
|
+
return processInfiniteLoop(effNode, currentBlock, S, cfgRules, processStatements);
|
|
230
72
|
}
|
|
231
73
|
if (isSwitchNode(type, cfgRules)) {
|
|
232
|
-
return processSwitch(effNode, currentBlock, S, cfgRules);
|
|
74
|
+
return processSwitch(effNode, currentBlock, S, cfgRules, processStatements);
|
|
233
75
|
}
|
|
234
76
|
if (cfgRules.tryNode && type === cfgRules.tryNode) {
|
|
235
|
-
return processTryCatch(effNode, currentBlock, S, cfgRules);
|
|
77
|
+
return processTryCatch(effNode, currentBlock, S, cfgRules, processStatements);
|
|
236
78
|
}
|
|
237
79
|
if (type === cfgRules.returnNode) {
|
|
238
80
|
currentBlock.endLine = effNode.startPosition.row + 1;
|
|
@@ -323,469 +165,6 @@ function processContinue(
|
|
|
323
165
|
return currentBlock;
|
|
324
166
|
}
|
|
325
167
|
|
|
326
|
-
function processIf(
|
|
327
|
-
ifStmt: TreeSitterNode,
|
|
328
|
-
currentBlock: CfgBlockInternal,
|
|
329
|
-
S: FuncState,
|
|
330
|
-
cfgRules: AnyRules,
|
|
331
|
-
): CfgBlockInternal {
|
|
332
|
-
currentBlock.endLine = ifStmt.startPosition.row + 1;
|
|
333
|
-
|
|
334
|
-
const condBlock = S.makeBlock(
|
|
335
|
-
'condition',
|
|
336
|
-
ifStmt.startPosition.row + 1,
|
|
337
|
-
ifStmt.startPosition.row + 1,
|
|
338
|
-
'if',
|
|
339
|
-
);
|
|
340
|
-
S.addEdge(currentBlock, condBlock, 'fallthrough');
|
|
341
|
-
|
|
342
|
-
const joinBlock = S.makeBlock('body');
|
|
343
|
-
|
|
344
|
-
const consequentField = cfgRules.ifConsequentField || 'consequence';
|
|
345
|
-
const consequent = ifStmt.childForFieldName(consequentField);
|
|
346
|
-
const trueBlock = S.makeBlock('branch_true', null, null, 'then');
|
|
347
|
-
S.addEdge(condBlock, trueBlock, 'branch_true');
|
|
348
|
-
const trueStmts = getBodyStatements(consequent, cfgRules);
|
|
349
|
-
const trueEnd = processStatements(trueStmts, trueBlock, S, cfgRules);
|
|
350
|
-
if (trueEnd) {
|
|
351
|
-
S.addEdge(trueEnd, joinBlock, 'fallthrough');
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
if (cfgRules.elifNode) {
|
|
355
|
-
processElifSiblings(ifStmt, condBlock, joinBlock, S, cfgRules);
|
|
356
|
-
} else {
|
|
357
|
-
processAlternative(ifStmt, condBlock, joinBlock, S, cfgRules);
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
return joinBlock;
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
function processAlternative(
|
|
364
|
-
ifStmt: TreeSitterNode,
|
|
365
|
-
condBlock: CfgBlockInternal,
|
|
366
|
-
joinBlock: CfgBlockInternal,
|
|
367
|
-
S: FuncState,
|
|
368
|
-
cfgRules: AnyRules,
|
|
369
|
-
): void {
|
|
370
|
-
const alternative = ifStmt.childForFieldName('alternative');
|
|
371
|
-
if (!alternative) {
|
|
372
|
-
S.addEdge(condBlock, joinBlock, 'branch_false');
|
|
373
|
-
return;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
if (cfgRules.elseViaAlternative && alternative.type !== cfgRules.elseClause) {
|
|
377
|
-
if (isIfNode(alternative.type, cfgRules)) {
|
|
378
|
-
const falseBlock = S.makeBlock('branch_false', null, null, 'else-if');
|
|
379
|
-
S.addEdge(condBlock, falseBlock, 'branch_false');
|
|
380
|
-
const elseIfEnd = processIf(alternative, falseBlock, S, cfgRules);
|
|
381
|
-
if (elseIfEnd) S.addEdge(elseIfEnd, joinBlock, 'fallthrough');
|
|
382
|
-
} else {
|
|
383
|
-
const falseBlock = S.makeBlock('branch_false', null, null, 'else');
|
|
384
|
-
S.addEdge(condBlock, falseBlock, 'branch_false');
|
|
385
|
-
const falseStmts = getBodyStatements(alternative, cfgRules);
|
|
386
|
-
const falseEnd = processStatements(falseStmts, falseBlock, S, cfgRules);
|
|
387
|
-
if (falseEnd) S.addEdge(falseEnd, joinBlock, 'fallthrough');
|
|
388
|
-
}
|
|
389
|
-
} else if (alternative.type === cfgRules.elseClause) {
|
|
390
|
-
const elseChildren: TreeSitterNode[] = [];
|
|
391
|
-
for (let i = 0; i < alternative.namedChildCount; i++) {
|
|
392
|
-
elseChildren.push(nn(alternative.namedChild(i)));
|
|
393
|
-
}
|
|
394
|
-
if (elseChildren.length === 1 && isIfNode(elseChildren[0]!.type, cfgRules)) {
|
|
395
|
-
const falseBlock = S.makeBlock('branch_false', null, null, 'else-if');
|
|
396
|
-
S.addEdge(condBlock, falseBlock, 'branch_false');
|
|
397
|
-
const elseIfEnd = processIf(elseChildren[0]!, falseBlock, S, cfgRules);
|
|
398
|
-
if (elseIfEnd) S.addEdge(elseIfEnd, joinBlock, 'fallthrough');
|
|
399
|
-
} else {
|
|
400
|
-
const falseBlock = S.makeBlock('branch_false', null, null, 'else');
|
|
401
|
-
S.addEdge(condBlock, falseBlock, 'branch_false');
|
|
402
|
-
const falseEnd = processStatements(elseChildren, falseBlock, S, cfgRules);
|
|
403
|
-
if (falseEnd) S.addEdge(falseEnd, joinBlock, 'fallthrough');
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
function processElifSiblings(
|
|
409
|
-
ifStmt: TreeSitterNode,
|
|
410
|
-
firstCondBlock: CfgBlockInternal,
|
|
411
|
-
joinBlock: CfgBlockInternal,
|
|
412
|
-
S: FuncState,
|
|
413
|
-
cfgRules: AnyRules,
|
|
414
|
-
): void {
|
|
415
|
-
let lastCondBlock = firstCondBlock;
|
|
416
|
-
let foundElse = false;
|
|
417
|
-
|
|
418
|
-
for (let i = 0; i < ifStmt.namedChildCount; i++) {
|
|
419
|
-
const child = nn(ifStmt.namedChild(i));
|
|
420
|
-
|
|
421
|
-
if (child.type === cfgRules.elifNode) {
|
|
422
|
-
const elifCondBlock = S.makeBlock(
|
|
423
|
-
'condition',
|
|
424
|
-
child.startPosition.row + 1,
|
|
425
|
-
child.startPosition.row + 1,
|
|
426
|
-
'else-if',
|
|
427
|
-
);
|
|
428
|
-
S.addEdge(lastCondBlock, elifCondBlock, 'branch_false');
|
|
429
|
-
|
|
430
|
-
const elifConsequentField = cfgRules.ifConsequentField || 'consequence';
|
|
431
|
-
const elifConsequent = child.childForFieldName(elifConsequentField);
|
|
432
|
-
const elifTrueBlock = S.makeBlock('branch_true', null, null, 'then');
|
|
433
|
-
S.addEdge(elifCondBlock, elifTrueBlock, 'branch_true');
|
|
434
|
-
const elifTrueStmts = getBodyStatements(elifConsequent, cfgRules);
|
|
435
|
-
const elifTrueEnd = processStatements(elifTrueStmts, elifTrueBlock, S, cfgRules);
|
|
436
|
-
if (elifTrueEnd) S.addEdge(elifTrueEnd, joinBlock, 'fallthrough');
|
|
437
|
-
|
|
438
|
-
lastCondBlock = elifCondBlock;
|
|
439
|
-
} else if (child.type === cfgRules.elseClause) {
|
|
440
|
-
const elseBlock = S.makeBlock('branch_false', null, null, 'else');
|
|
441
|
-
S.addEdge(lastCondBlock, elseBlock, 'branch_false');
|
|
442
|
-
|
|
443
|
-
const elseBody = child.childForFieldName('body');
|
|
444
|
-
let elseStmts: TreeSitterNode[];
|
|
445
|
-
if (elseBody) {
|
|
446
|
-
elseStmts = getBodyStatements(elseBody, cfgRules);
|
|
447
|
-
} else {
|
|
448
|
-
elseStmts = [];
|
|
449
|
-
for (let j = 0; j < child.namedChildCount; j++) {
|
|
450
|
-
elseStmts.push(nn(child.namedChild(j)));
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
const elseEnd = processStatements(elseStmts, elseBlock, S, cfgRules);
|
|
454
|
-
if (elseEnd) S.addEdge(elseEnd, joinBlock, 'fallthrough');
|
|
455
|
-
|
|
456
|
-
foundElse = true;
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
if (!foundElse) {
|
|
461
|
-
S.addEdge(lastCondBlock, joinBlock, 'branch_false');
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
function processForLoop(
|
|
466
|
-
forStmt: TreeSitterNode,
|
|
467
|
-
currentBlock: CfgBlockInternal,
|
|
468
|
-
S: FuncState,
|
|
469
|
-
cfgRules: AnyRules,
|
|
470
|
-
): CfgBlockInternal {
|
|
471
|
-
const headerBlock = S.makeBlock(
|
|
472
|
-
'loop_header',
|
|
473
|
-
forStmt.startPosition.row + 1,
|
|
474
|
-
forStmt.startPosition.row + 1,
|
|
475
|
-
'for',
|
|
476
|
-
);
|
|
477
|
-
S.addEdge(currentBlock, headerBlock, 'fallthrough');
|
|
478
|
-
|
|
479
|
-
const loopExitBlock = S.makeBlock('body');
|
|
480
|
-
const loopCtx: LoopCtx = { headerBlock, exitBlock: loopExitBlock };
|
|
481
|
-
S.loopStack.push(loopCtx);
|
|
482
|
-
registerLabelCtx(S, headerBlock, loopExitBlock);
|
|
483
|
-
|
|
484
|
-
const body = forStmt.childForFieldName('body');
|
|
485
|
-
const bodyBlock = S.makeBlock('loop_body');
|
|
486
|
-
S.addEdge(headerBlock, bodyBlock, 'branch_true');
|
|
487
|
-
|
|
488
|
-
const bodyStmts = getBodyStatements(body, cfgRules);
|
|
489
|
-
const bodyEnd = processStatements(bodyStmts, bodyBlock, S, cfgRules);
|
|
490
|
-
if (bodyEnd) S.addEdge(bodyEnd, headerBlock, 'loop_back');
|
|
491
|
-
|
|
492
|
-
S.addEdge(headerBlock, loopExitBlock, 'loop_exit');
|
|
493
|
-
S.loopStack.pop();
|
|
494
|
-
return loopExitBlock;
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
function processWhileLoop(
|
|
498
|
-
whileStmt: TreeSitterNode,
|
|
499
|
-
currentBlock: CfgBlockInternal,
|
|
500
|
-
S: FuncState,
|
|
501
|
-
cfgRules: AnyRules,
|
|
502
|
-
): CfgBlockInternal {
|
|
503
|
-
const headerBlock = S.makeBlock(
|
|
504
|
-
'loop_header',
|
|
505
|
-
whileStmt.startPosition.row + 1,
|
|
506
|
-
whileStmt.startPosition.row + 1,
|
|
507
|
-
'while',
|
|
508
|
-
);
|
|
509
|
-
S.addEdge(currentBlock, headerBlock, 'fallthrough');
|
|
510
|
-
|
|
511
|
-
const loopExitBlock = S.makeBlock('body');
|
|
512
|
-
const loopCtx: LoopCtx = { headerBlock, exitBlock: loopExitBlock };
|
|
513
|
-
S.loopStack.push(loopCtx);
|
|
514
|
-
registerLabelCtx(S, headerBlock, loopExitBlock);
|
|
515
|
-
|
|
516
|
-
const body = whileStmt.childForFieldName('body');
|
|
517
|
-
const bodyBlock = S.makeBlock('loop_body');
|
|
518
|
-
S.addEdge(headerBlock, bodyBlock, 'branch_true');
|
|
519
|
-
|
|
520
|
-
const bodyStmts = getBodyStatements(body, cfgRules);
|
|
521
|
-
const bodyEnd = processStatements(bodyStmts, bodyBlock, S, cfgRules);
|
|
522
|
-
if (bodyEnd) S.addEdge(bodyEnd, headerBlock, 'loop_back');
|
|
523
|
-
|
|
524
|
-
S.addEdge(headerBlock, loopExitBlock, 'loop_exit');
|
|
525
|
-
S.loopStack.pop();
|
|
526
|
-
return loopExitBlock;
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
function processDoWhileLoop(
|
|
530
|
-
doStmt: TreeSitterNode,
|
|
531
|
-
currentBlock: CfgBlockInternal,
|
|
532
|
-
S: FuncState,
|
|
533
|
-
cfgRules: AnyRules,
|
|
534
|
-
): CfgBlockInternal {
|
|
535
|
-
const bodyBlock = S.makeBlock('loop_body', doStmt.startPosition.row + 1, null, 'do');
|
|
536
|
-
S.addEdge(currentBlock, bodyBlock, 'fallthrough');
|
|
537
|
-
|
|
538
|
-
const condBlock = S.makeBlock('loop_header', null, null, 'do-while');
|
|
539
|
-
const loopExitBlock = S.makeBlock('body');
|
|
540
|
-
|
|
541
|
-
const loopCtx: LoopCtx = { headerBlock: condBlock, exitBlock: loopExitBlock };
|
|
542
|
-
S.loopStack.push(loopCtx);
|
|
543
|
-
registerLabelCtx(S, condBlock, loopExitBlock);
|
|
544
|
-
|
|
545
|
-
const body = doStmt.childForFieldName('body');
|
|
546
|
-
const bodyStmts = getBodyStatements(body, cfgRules);
|
|
547
|
-
const bodyEnd = processStatements(bodyStmts, bodyBlock, S, cfgRules);
|
|
548
|
-
if (bodyEnd) S.addEdge(bodyEnd, condBlock, 'fallthrough');
|
|
549
|
-
|
|
550
|
-
S.addEdge(condBlock, bodyBlock, 'loop_back');
|
|
551
|
-
S.addEdge(condBlock, loopExitBlock, 'loop_exit');
|
|
552
|
-
|
|
553
|
-
S.loopStack.pop();
|
|
554
|
-
return loopExitBlock;
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
function processInfiniteLoop(
|
|
558
|
-
loopStmt: TreeSitterNode,
|
|
559
|
-
currentBlock: CfgBlockInternal,
|
|
560
|
-
S: FuncState,
|
|
561
|
-
cfgRules: AnyRules,
|
|
562
|
-
): CfgBlockInternal {
|
|
563
|
-
const headerBlock = S.makeBlock(
|
|
564
|
-
'loop_header',
|
|
565
|
-
loopStmt.startPosition.row + 1,
|
|
566
|
-
loopStmt.startPosition.row + 1,
|
|
567
|
-
'loop',
|
|
568
|
-
);
|
|
569
|
-
S.addEdge(currentBlock, headerBlock, 'fallthrough');
|
|
570
|
-
|
|
571
|
-
const loopExitBlock = S.makeBlock('body');
|
|
572
|
-
const loopCtx: LoopCtx = { headerBlock, exitBlock: loopExitBlock };
|
|
573
|
-
S.loopStack.push(loopCtx);
|
|
574
|
-
registerLabelCtx(S, headerBlock, loopExitBlock);
|
|
575
|
-
|
|
576
|
-
const body = loopStmt.childForFieldName('body');
|
|
577
|
-
const bodyBlock = S.makeBlock('loop_body');
|
|
578
|
-
S.addEdge(headerBlock, bodyBlock, 'branch_true');
|
|
579
|
-
|
|
580
|
-
const bodyStmts = getBodyStatements(body, cfgRules);
|
|
581
|
-
const bodyEnd = processStatements(bodyStmts, bodyBlock, S, cfgRules);
|
|
582
|
-
if (bodyEnd) S.addEdge(bodyEnd, headerBlock, 'loop_back');
|
|
583
|
-
|
|
584
|
-
S.loopStack.pop();
|
|
585
|
-
return loopExitBlock;
|
|
586
|
-
}
|
|
587
|
-
|
|
588
|
-
function processSwitch(
|
|
589
|
-
switchStmt: TreeSitterNode,
|
|
590
|
-
currentBlock: CfgBlockInternal,
|
|
591
|
-
S: FuncState,
|
|
592
|
-
cfgRules: AnyRules,
|
|
593
|
-
): CfgBlockInternal {
|
|
594
|
-
currentBlock.endLine = switchStmt.startPosition.row + 1;
|
|
595
|
-
|
|
596
|
-
const switchHeader = S.makeBlock(
|
|
597
|
-
'condition',
|
|
598
|
-
switchStmt.startPosition.row + 1,
|
|
599
|
-
switchStmt.startPosition.row + 1,
|
|
600
|
-
'switch',
|
|
601
|
-
);
|
|
602
|
-
S.addEdge(currentBlock, switchHeader, 'fallthrough');
|
|
603
|
-
|
|
604
|
-
const joinBlock = S.makeBlock('body');
|
|
605
|
-
const switchCtx: LoopCtx = { headerBlock: switchHeader, exitBlock: joinBlock };
|
|
606
|
-
S.loopStack.push(switchCtx);
|
|
607
|
-
|
|
608
|
-
const switchBody = switchStmt.childForFieldName('body');
|
|
609
|
-
const container = switchBody || switchStmt;
|
|
610
|
-
|
|
611
|
-
let hasDefault = false;
|
|
612
|
-
for (let i = 0; i < container.namedChildCount; i++) {
|
|
613
|
-
const caseClause = nn(container.namedChild(i));
|
|
614
|
-
|
|
615
|
-
const isDefault = caseClause.type === cfgRules.defaultNode;
|
|
616
|
-
const isCase = isDefault || isCaseNode(caseClause.type, cfgRules);
|
|
617
|
-
if (!isCase) continue;
|
|
618
|
-
|
|
619
|
-
const caseLabel = isDefault ? 'default' : 'case';
|
|
620
|
-
const caseBlock = S.makeBlock('case', caseClause.startPosition.row + 1, null, caseLabel);
|
|
621
|
-
S.addEdge(switchHeader, caseBlock, isDefault ? 'branch_false' : 'branch_true');
|
|
622
|
-
if (isDefault) hasDefault = true;
|
|
623
|
-
|
|
624
|
-
const caseStmts = extractCaseBody(caseClause, cfgRules);
|
|
625
|
-
const caseEnd = processStatements(caseStmts, caseBlock, S, cfgRules);
|
|
626
|
-
if (caseEnd) S.addEdge(caseEnd, joinBlock, 'fallthrough');
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
if (!hasDefault) {
|
|
630
|
-
S.addEdge(switchHeader, joinBlock, 'branch_false');
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
S.loopStack.pop();
|
|
634
|
-
return joinBlock;
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
function extractCaseBody(caseClause: TreeSitterNode, cfgRules: AnyRules): TreeSitterNode[] {
|
|
638
|
-
const caseBodyNode =
|
|
639
|
-
caseClause.childForFieldName('body') || caseClause.childForFieldName('consequence');
|
|
640
|
-
if (caseBodyNode) {
|
|
641
|
-
return getBodyStatements(caseBodyNode, cfgRules);
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
const stmts: TreeSitterNode[] = [];
|
|
645
|
-
const valueNode = caseClause.childForFieldName('value');
|
|
646
|
-
const patternNode = caseClause.childForFieldName('pattern');
|
|
647
|
-
for (let j = 0; j < caseClause.namedChildCount; j++) {
|
|
648
|
-
const child = nn(caseClause.namedChild(j));
|
|
649
|
-
if (child !== valueNode && child !== patternNode && child.type !== 'switch_label') {
|
|
650
|
-
if (child.type === 'statement_list') {
|
|
651
|
-
for (let k = 0; k < child.namedChildCount; k++) {
|
|
652
|
-
stmts.push(nn(child.namedChild(k)));
|
|
653
|
-
}
|
|
654
|
-
} else {
|
|
655
|
-
stmts.push(child);
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
}
|
|
659
|
-
return stmts;
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
function processTryCatch(
|
|
663
|
-
tryStmt: TreeSitterNode,
|
|
664
|
-
currentBlock: CfgBlockInternal,
|
|
665
|
-
S: FuncState,
|
|
666
|
-
cfgRules: AnyRules,
|
|
667
|
-
): CfgBlockInternal {
|
|
668
|
-
currentBlock.endLine = tryStmt.startPosition.row + 1;
|
|
669
|
-
|
|
670
|
-
const joinBlock = S.makeBlock('body');
|
|
671
|
-
|
|
672
|
-
const tryBody = tryStmt.childForFieldName('body');
|
|
673
|
-
let tryBodyStart: number;
|
|
674
|
-
let tryStmts: TreeSitterNode[];
|
|
675
|
-
if (tryBody) {
|
|
676
|
-
tryBodyStart = tryBody.startPosition.row + 1;
|
|
677
|
-
tryStmts = getBodyStatements(tryBody, cfgRules);
|
|
678
|
-
} else {
|
|
679
|
-
tryBodyStart = tryStmt.startPosition.row + 1;
|
|
680
|
-
tryStmts = [];
|
|
681
|
-
for (let i = 0; i < tryStmt.namedChildCount; i++) {
|
|
682
|
-
const child = nn(tryStmt.namedChild(i));
|
|
683
|
-
if (cfgRules.catchNode && child.type === cfgRules.catchNode) continue;
|
|
684
|
-
if (cfgRules.finallyNode && child.type === cfgRules.finallyNode) continue;
|
|
685
|
-
tryStmts.push(child);
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
const tryBlock = S.makeBlock('body', tryBodyStart, null, 'try');
|
|
690
|
-
S.addEdge(currentBlock, tryBlock, 'fallthrough');
|
|
691
|
-
const tryEnd = processStatements(tryStmts, tryBlock, S, cfgRules);
|
|
692
|
-
|
|
693
|
-
const { catchHandler, finallyHandler } = findTryHandlers(tryStmt, cfgRules);
|
|
694
|
-
|
|
695
|
-
if (catchHandler) {
|
|
696
|
-
processCatchHandler(catchHandler, tryBlock, tryEnd, finallyHandler, joinBlock, S, cfgRules);
|
|
697
|
-
} else if (finallyHandler) {
|
|
698
|
-
processFinallyOnly(finallyHandler, tryEnd, joinBlock, S, cfgRules);
|
|
699
|
-
} else {
|
|
700
|
-
if (tryEnd) S.addEdge(tryEnd, joinBlock, 'fallthrough');
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
return joinBlock;
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
function findTryHandlers(
|
|
707
|
-
tryStmt: TreeSitterNode,
|
|
708
|
-
cfgRules: AnyRules,
|
|
709
|
-
): { catchHandler: TreeSitterNode | null; finallyHandler: TreeSitterNode | null } {
|
|
710
|
-
let catchHandler: TreeSitterNode | null = null;
|
|
711
|
-
let finallyHandler: TreeSitterNode | null = null;
|
|
712
|
-
for (let i = 0; i < tryStmt.namedChildCount; i++) {
|
|
713
|
-
const child = nn(tryStmt.namedChild(i));
|
|
714
|
-
if (cfgRules.catchNode && child.type === cfgRules.catchNode) catchHandler = child;
|
|
715
|
-
if (cfgRules.finallyNode && child.type === cfgRules.finallyNode) finallyHandler = child;
|
|
716
|
-
}
|
|
717
|
-
return { catchHandler, finallyHandler };
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
function processCatchHandler(
|
|
721
|
-
catchHandler: TreeSitterNode,
|
|
722
|
-
tryBlock: CfgBlockInternal,
|
|
723
|
-
tryEnd: CfgBlockInternal | null,
|
|
724
|
-
finallyHandler: TreeSitterNode | null,
|
|
725
|
-
joinBlock: CfgBlockInternal,
|
|
726
|
-
S: FuncState,
|
|
727
|
-
cfgRules: AnyRules,
|
|
728
|
-
): void {
|
|
729
|
-
const catchBlock = S.makeBlock('catch', catchHandler.startPosition.row + 1, null, 'catch');
|
|
730
|
-
S.addEdge(tryBlock, catchBlock, 'exception');
|
|
731
|
-
|
|
732
|
-
const catchBodyNode = catchHandler.childForFieldName('body');
|
|
733
|
-
let catchStmts: TreeSitterNode[];
|
|
734
|
-
if (catchBodyNode) {
|
|
735
|
-
catchStmts = getBodyStatements(catchBodyNode, cfgRules);
|
|
736
|
-
} else {
|
|
737
|
-
catchStmts = [];
|
|
738
|
-
for (let i = 0; i < catchHandler.namedChildCount; i++) {
|
|
739
|
-
catchStmts.push(nn(catchHandler.namedChild(i)));
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
const catchEnd = processStatements(catchStmts, catchBlock, S, cfgRules);
|
|
743
|
-
|
|
744
|
-
if (finallyHandler) {
|
|
745
|
-
const finallyBlock = S.makeBlock(
|
|
746
|
-
'finally',
|
|
747
|
-
finallyHandler.startPosition.row + 1,
|
|
748
|
-
null,
|
|
749
|
-
'finally',
|
|
750
|
-
);
|
|
751
|
-
if (tryEnd) S.addEdge(tryEnd, finallyBlock, 'fallthrough');
|
|
752
|
-
if (catchEnd) S.addEdge(catchEnd, finallyBlock, 'fallthrough');
|
|
753
|
-
|
|
754
|
-
const finallyBodyNode = finallyHandler.childForFieldName('body');
|
|
755
|
-
const finallyStmts = finallyBodyNode
|
|
756
|
-
? getBodyStatements(finallyBodyNode, cfgRules)
|
|
757
|
-
: getBodyStatements(finallyHandler, cfgRules);
|
|
758
|
-
const finallyEnd = processStatements(finallyStmts, finallyBlock, S, cfgRules);
|
|
759
|
-
if (finallyEnd) S.addEdge(finallyEnd, joinBlock, 'fallthrough');
|
|
760
|
-
} else {
|
|
761
|
-
if (tryEnd) S.addEdge(tryEnd, joinBlock, 'fallthrough');
|
|
762
|
-
if (catchEnd) S.addEdge(catchEnd, joinBlock, 'fallthrough');
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
|
|
766
|
-
function processFinallyOnly(
|
|
767
|
-
finallyHandler: TreeSitterNode,
|
|
768
|
-
tryEnd: CfgBlockInternal | null,
|
|
769
|
-
joinBlock: CfgBlockInternal,
|
|
770
|
-
S: FuncState,
|
|
771
|
-
cfgRules: AnyRules,
|
|
772
|
-
): void {
|
|
773
|
-
const finallyBlock = S.makeBlock(
|
|
774
|
-
'finally',
|
|
775
|
-
finallyHandler.startPosition.row + 1,
|
|
776
|
-
null,
|
|
777
|
-
'finally',
|
|
778
|
-
);
|
|
779
|
-
if (tryEnd) S.addEdge(tryEnd, finallyBlock, 'fallthrough');
|
|
780
|
-
|
|
781
|
-
const finallyBodyNode = finallyHandler.childForFieldName('body');
|
|
782
|
-
const finallyStmts = finallyBodyNode
|
|
783
|
-
? getBodyStatements(finallyBodyNode, cfgRules)
|
|
784
|
-
: getBodyStatements(finallyHandler, cfgRules);
|
|
785
|
-
const finallyEnd = processStatements(finallyStmts, finallyBlock, S, cfgRules);
|
|
786
|
-
if (finallyEnd) S.addEdge(finallyEnd, joinBlock, 'fallthrough');
|
|
787
|
-
}
|
|
788
|
-
|
|
789
168
|
function processFunctionBody(funcNode: TreeSitterNode, S: FuncState, cfgRules: AnyRules): void {
|
|
790
169
|
const body = funcNode.childForFieldName('body');
|
|
791
170
|
if (!body) {
|