Skip to content

Instantly share code, notes, and snippets.

@iljavs
Last active May 8, 2022 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iljavs/7d20988922c0c0768c0272863c677acc to your computer and use it in GitHub Desktop.
Save iljavs/7d20988922c0c0768c0272863c677acc to your computer and use it in GitHub Desktop.
find recursive functions in c files in a directory using cflow
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
func recursiveoutput(out []byte) {
// split the output into lines
lines := strings.Split(string(out), "\n")
// loop through the lines
for _, line := range lines {
// does the line contain "recursive:"
if strings.Contains(line, "recursive:") {
// print the line
fmt.Println(line)
}
}
return
}
func cflow(path string) {
// call the cflow command (/usr/bin/cflow) and capture the output
out, err := exec.Command("/usr/bin/cflow", path).Output()
if err != nil {
fmt.Printf("%s", err)
return
}
recursiveoutput(out)
}
func main() {
// get the first argument given to the program
dir := os.Args[1]
// recursively walk the directory using filepath.Walk
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
} else if info.IsDir() {
return nil
} else {
// array of strings
ext := []string{".c", ".h", ".cc", ".cpp", ".cxx", ".hpp", ".hxx"}
// does the file end with one of the extensions
for _, e := range ext {
if strings.HasSuffix(path, e) {
// call the cflow function
cflow(path)
}
}
}
return nil
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment