Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding src command to argot cli to show source code. #134

Merged
merged 2 commits into from
Jan 23, 2025
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
6 changes: 3 additions & 3 deletions .github/workflows/license-scanning.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
Expand All @@ -34,8 +34,8 @@ jobs:
run: |
go-licenses report --include_tests ./analysis ./internal/* ./cmd/* > licenses_report.csv
- name: Archive license report
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: licenses-report
retention-days: 3
path: licenses_report.csv
path: licenses_report.csv
54 changes: 54 additions & 0 deletions cmd/argot/cli/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cli
import (
"bytes"
"context"
"go/printer"
"os"
"os/exec"
"regexp"
Expand All @@ -29,6 +30,7 @@ import (
"github.com/awslabs/ar-go-tools/analysis/render"
"github.com/awslabs/ar-go-tools/analysis/summaries"
"github.com/awslabs/ar-go-tools/analysis/taint"
"github.com/awslabs/ar-go-tools/internal/formatutil"
ftu "github.com/awslabs/ar-go-tools/internal/formatutil"
"github.com/awslabs/ar-go-tools/internal/funcutil"
"golang.org/x/term"
Expand Down Expand Up @@ -217,6 +219,58 @@ func cmdSummary(tt *term.Terminal, c *dataflow.State, command Command, _ bool) b
return false
}

// cmdShowSource prints the source (AST representation) of all the functions matching a given regex
func cmdSrc(tt *term.Terminal, c *dataflow.State, command Command, withTest bool) bool {
if c == nil {
writeFmt(tt, "\t- %s%s%s : print the source code of a function.\n"+
"\t src regex prints the AST representation of the function matching the regex\n"+
"\t Example:\n", tt.Escape.Blue, cmdSrcName, tt.Escape.Reset)
writeFmt(tt, "\t > %s command-line-arguments.main\n", cmdSrcName)
return false
}

if len(command.Args) < 1 {
if state.CurrentFunction != nil {
astNode := state.CurrentFunction.Syntax()
if astNode == nil {
WriteErr(tt, "%s has no syntax.", formatutil.Bold(state.CurrentFunction.String()))
} else {
WriteSuccess(tt, "<<< Source for %s", formatutil.Bold(state.CurrentFunction.String()))
printer.Fprint(tt, c.Program.Fset, astNode)
writeFmt(tt, "\n")
WriteSuccess(tt, "End of source for %s >>>", state.CurrentFunction.String())
writeFmt(tt, "\n")
}

} else {
WriteErr(tt, "Need at least one function to show source for.")
cmdSrc(tt, nil, command, withTest)
}

return false
}
target, err := regexp.Compile(command.Args[0])
if err != nil {
regexErr(tt, command.Args[0], err)
return false
}

funcs := findFunc(c, target)
for _, f := range funcs {
astNode := f.Syntax()
if astNode == nil {
WriteErr(tt, "%s has no syntax.", formatutil.Bold(f.String()))
} else {
WriteSuccess(tt, "<<< Source for %s", formatutil.Bold(f.String()))
printer.Fprint(tt, c.Program.Fset, astNode)
writeFmt(tt, "\n")
WriteSuccess(tt, "End of source for %s >>>", f.String())
writeFmt(tt, "\n")
}
}
return false
}

// cmdSummarize runs the intra-procedural analysis.
func cmdSummarize(tt *term.Terminal, c *dataflow.State, command Command, _ bool) bool {
if c == nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/argot/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ var commands = map[string]func(tt *term.Terminal, s *dataflow.State, command Com
cmdShowSsaName: cmdShowSsa,
cmdShowEscapeName: cmdShowEscape,
cmdShowDataflowName: cmdShowDataflow,
cmdSrcName: cmdSrc,
cmdSsaInstrName: cmdSsaInstr,
cmdSsaValueName: cmdSsaValue,
cmdStateName: cmdState,
Expand Down
1 change: 1 addition & 0 deletions cmd/argot/cli/defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const (
cmdShowDataflowName = "showdataflow"
cmdShowEscapeName = "showescape"
cmdShowSsaName = "showssa"
cmdSrcName = "src"
cmdSsaInstrName = "ssainstr"
cmdSsaValueName = "ssaval"
cmdStateName = "state?"
Expand Down
Loading