Don't pass file paths as strings.

Instead of passing file paths as strings, now pass os.File or directory paths
for compile_commands.json since it's always named that.

PiperOrigin-RevId: 657349644
This commit is contained in:
Anthony Cui
2024-07-29 15:44:00 -07:00
committed by gVisor bot
parent 714f1590e1
commit 392e838736
2 changed files with 11 additions and 15 deletions
@@ -18,6 +18,7 @@ import (
"encoding/json"
"fmt"
"os"
"path"
)
// ClangASTConfig is the format for compilation_commands.json.
@@ -42,11 +43,12 @@ func NewParserConfig(directory, filename string, includes []string) ClangASTConf
}
}
// CreateCompileCommandsFile writes the given config to file.
func CreateCompileCommandsFile(config []ClangASTConfig, path string) error {
f, err := os.Create(path)
// CreateCompileCommandsFile creates a new compile_commands.json file in the given directory, and
// writes config to it.
func CreateCompileCommandsFile(dir string, config []ClangASTConfig) error {
f, err := os.Create(path.Join(dir, "compile_commands.json"))
if err != nil {
return fmt.Errorf("failed to create %s: %w", path, err)
return fmt.Errorf("failed to create compile_commands.json: %w", err)
}
defer f.Close()
+5 -11
View File
@@ -17,7 +17,7 @@ package parser
import (
"bufio"
"fmt"
"os"
"io"
"path/filepath"
)
@@ -83,19 +83,13 @@ func (d *DriverSourceDir) GetUVMIncludePaths() []string {
}
// WriteIncludeFile writes an cc file at file that includes all the given sources.
func WriteIncludeFile(sources []string, path string) error {
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("failed to create include file: %w", err)
}
defer f.Close()
w := bufio.NewWriter(f)
func WriteIncludeFile(sources []string, w io.Writer) error {
bufW := bufio.NewWriter(w)
for _, source := range sources {
if _, err := w.WriteString(fmt.Sprintf("#include \"%s\"\n", source)); err != nil {
if _, err := bufW.WriteString(fmt.Sprintf("#include \"%s\"\n", source)); err != nil {
return fmt.Errorf("failed to write to include file: %w", err)
}
}
return w.Flush()
return bufW.Flush()
}