From 392e838736f0ae12f730bacf7b658e9426342578 Mon Sep 17 00:00:00 2001 From: Anthony Cui Date: Mon, 29 Jul 2024 15:40:07 -0700 Subject: [PATCH] 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 --- .../nvidia_driver_differ/parser/clang_config.go | 10 ++++++---- tools/nvidia_driver_differ/parser/sources.go | 16 +++++----------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/tools/nvidia_driver_differ/parser/clang_config.go b/tools/nvidia_driver_differ/parser/clang_config.go index 675d90342..7348403ea 100644 --- a/tools/nvidia_driver_differ/parser/clang_config.go +++ b/tools/nvidia_driver_differ/parser/clang_config.go @@ -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() diff --git a/tools/nvidia_driver_differ/parser/sources.go b/tools/nvidia_driver_differ/parser/sources.go index b5cb427d9..dd91f693c 100644 --- a/tools/nvidia_driver_differ/parser/sources.go +++ b/tools/nvidia_driver_differ/parser/sources.go @@ -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() }