Created
March 5, 2024 17:19
-
-
Save SilverMight/329dd869d30883842b5cb6c4bb3164e6 to your computer and use it in GitHub Desktop.
Compile commands parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io" | |
"os" | |
"os/exec" | |
) | |
type TranslationUnit struct { | |
Arguments []string `json:"arguments"` | |
File string `json:"file"` | |
} | |
func RunCallFinder(units []TranslationUnit, callFinderPath string, outputFile string) error { | |
file, err := os.Create(outputFile) | |
if err != nil { | |
return err | |
} | |
defer file.Close() | |
for _, unit := range units { | |
args := append(unit.Arguments[:len(unit.Arguments)-1], unit.File) | |
program := exec.Command(callFinderPath, args...) | |
fmt.Println("Running: ", program.String()) | |
program.Stdout = file | |
program.Stderr = os.Stderr | |
err := program.Run() | |
if err != nil { | |
return err | |
} | |
} | |
return nil | |
} | |
func GetTranslationUnits(reader io.Reader) ([]TranslationUnit, error) { | |
var units []TranslationUnit | |
err := json.NewDecoder(reader).Decode(&units) | |
if err != nil { | |
return nil, err | |
} | |
// don't care about compiler | |
for i := 0; i < len(units); i++ { | |
units[i].Arguments = units[i].Arguments[1:] | |
} | |
return units, nil | |
} | |
func main() { | |
if len(os.Args) < 3 { | |
fmt.Println("Usage: call_finder <compile_commands.json> <call_finder> <output>") | |
return | |
} | |
callFinder := os.Args[2] | |
compileCommands := os.Args[1] | |
output := os.Args[3] | |
file, _ := os.Open(compileCommands) | |
units, err := GetTranslationUnits(file) | |
if err != nil { | |
fmt.Printf("Failed to decode: %v\n", err) | |
return | |
} | |
err = RunCallFinder(units, callFinder, output) | |
if err != nil { | |
fmt.Printf("Failed to run call finder: %v\n", err) | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment