Skip to content

Commit

Permalink
Remove AST code
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmetzman committed Jun 17, 2024
1 parent c936102 commit af89694
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 125 deletions.
64 changes: 1 addition & 63 deletions infra/base-images/base-builder/jcc/jcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"os/exec"
"path/filepath"
"regexp"
"slices"
"strings"
)

Expand Down Expand Up @@ -169,63 +168,6 @@ func CorrectMissingHeaders(bin string, cmd []string) ([]string, bool, error) {
return cmd, false, nil
}

func EnsureDir(dirPath string) {
// Checks if a path is an existing directory, otherwise create one.
if pathInfo, err := os.Stat(dirPath); err == nil {
if isDir := pathInfo.IsDir(); !isDir {
panic(dirPath + " exists but is not a directory.")
}
} else if errors.Is(err, fs.ErrNotExist) {
if err := os.MkdirAll(dirPath, 0755); err != nil {
panic("Failed to create directory: " + dirPath + ".")
}
fmt.Println("Created directory: " + dirPath + ".")
} else {
panic("An error occurred in os.Stat(" + dirPath + "): " + err.Error())
}
}

func GenerateAST(bin string, args []string, filePath string) {
// Generates AST.
outFile, err := os.Create(filePath)
if err != nil {
fmt.Println(err)
}
defer outFile.Close()

cmd := exec.Command(bin, args...)
cmd.Stdout = outFile
cmd.Run()
}

func GenerateASTs(bin string, args []string, astDir string) {
// Generates an AST for each C/CPP file in the command.
// Cannot save AST when astDir is not available.
EnsureDir(astDir)

// Target file suffixes.
suffixes := []string{".cpp", ".cc", ".cxx", ".c++", ".c", ".h", ".hpp"}
// C/CPP targets in the command.
targetFiles := []string{}
// Flags to generate AST.
flags := []string{"-Xclang", "-ast-dump=json", "-fsyntax-only"}
for _, arg := range args {
targetFileExt := strings.ToLower(filepath.Ext(arg))
if slices.Contains(suffixes, targetFileExt) {
targetFiles = append(targetFiles, arg)
continue
}
flags = append(flags, arg)
}

// Generate an AST for each target file. Skips AST generation when a
// command has no target file (e.g., during linking).
for _, targetFile := range targetFiles {
filePath := filepath.Join(astDir, fmt.Sprintf("%s.ast", filepath.Base(targetFile)))
GenerateAST(bin, append(flags, targetFile), filePath)
}
}

func ExecBuildCommand(bin string, args []string) (int, string, string) {
// Executes the original command.
cmd := exec.Command(bin, args...)
Expand All @@ -238,10 +180,6 @@ func ExecBuildCommand(bin string, args []string) (int, string, string) {
}

func Compile(bin string, args []string) (int, string, string) {
// Generate ASTs f we define this ENV var.
if astDir := os.Getenv("JCC_GENERATE_AST_DIR"); astDir != "" {
GenerateASTs(bin, args, astDir)
}
// Run the actual command.
return ExecBuildCommand(bin, args)
}
Expand Down Expand Up @@ -360,7 +298,7 @@ func WriteStdErrOut(args []string, outstr string, errstr string) {
fmt.Print(outstr)
fmt.Fprint(os.Stderr, errstr)
// Record what compile args produced the error and the error itself in log file.
AppendStringToFile("/workspace/err.log", fmt.Sprintf("%s\n", args) + errstr)
AppendStringToFile("/workspace/err.log", fmt.Sprintf("%s\n", args)+errstr)
}

func main() {
Expand Down
62 changes: 0 additions & 62 deletions infra/base-images/base-builder/jcc/jcc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"os/exec"
"path/filepath"
"regexp"
"slices"
"strings"
)

Expand Down Expand Up @@ -170,63 +169,6 @@ func CorrectMissingHeaders(bin string, cmd []string) ([]string, bool, error) {
return cmd, false, nil
}

func EnsureDir(dirPath string) {
// Checks if a path is an existing directory, otherwise create one.
if pathInfo, err := os.Stat(dirPath); err == nil {
if isDir := pathInfo.IsDir(); !isDir {
panic(dirPath + " exists but is not a directory.")
}
} else if errors.Is(err, fs.ErrNotExist) {
if err := os.MkdirAll(dirPath, 0755); err != nil {
panic("Failed to create directory: " + dirPath + ".")
}
fmt.Println("Created directory: " + dirPath + ".")
} else {
panic("An error occurred in os.Stat(" + dirPath + "): " + err.Error())
}
}

func GenerateAST(bin string, args []string, filePath string) {
// Generates AST.
outFile, err := os.Create(filePath)
if err != nil {
fmt.Println(err)
}
defer outFile.Close()

cmd := exec.Command(bin, args...)
cmd.Stdout = outFile
cmd.Run()
}

func GenerateASTs(bin string, args []string, astDir string) {
// Generates an AST for each C/CPP file in the command.
// Cannot save AST when astDir is not available.
EnsureDir(astDir)

// Target file suffixes.
suffixes := []string{".cpp", ".cc", ".cxx", ".c++", ".c", ".h", ".hpp"}
// C/CPP targets in the command.
targetFiles := []string{}
// Flags to generate AST.
flags := []string{"-Xclang", "-ast-dump=json", "-fsyntax-only"}
for _, arg := range args {
targetFileExt := strings.ToLower(filepath.Ext(arg))
if slices.Contains(suffixes, targetFileExt) {
targetFiles = append(targetFiles, arg)
continue
}
flags = append(flags, arg)
}

// Generate an AST for each target file. Skips AST generation when a
// command has no target file (e.g., during linking).
for _, targetFile := range targetFiles {
filePath := filepath.Join(astDir, fmt.Sprintf("%s.ast", filepath.Base(targetFile)))
GenerateAST(bin, append(flags, targetFile), filePath)
}
}

func ExecBuildCommand(bin string, args []string) (int, string, string) {
// Executes the original command.
cmd := exec.Command(bin, args...)
Expand All @@ -239,10 +181,6 @@ func ExecBuildCommand(bin string, args []string) (int, string, string) {
}

func Compile(bin string, args []string) (int, string, string) {
// Generate ASTs f we define this ENV var.
if astDir := os.Getenv("JCC_GENERATE_AST_DIR"); astDir != "" {
GenerateASTs(bin, args, astDir)
}
// Run the actual command.
return ExecBuildCommand(bin, args)
}
Expand Down

0 comments on commit af89694

Please sign in to comment.