From f9e41008362f37c6750d67665fefb18e6d83ad50 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 6 Feb 2024 10:18:39 +0100 Subject: [PATCH] polish the stdout/stderr assignments --- .../builder/internal/preprocessor/ctags.go | 33 ++++++++++--------- internal/arduino/builder/preprocess_sketch.go | 5 ++- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/internal/arduino/builder/internal/preprocessor/ctags.go b/internal/arduino/builder/internal/preprocessor/ctags.go index 44e5ebb77b1..3d627286146 100644 --- a/internal/arduino/builder/internal/preprocessor/ctags.go +++ b/internal/arduino/builder/internal/preprocessor/ctags.go @@ -42,7 +42,8 @@ var DebugPreprocessor bool // PreprocessSketchWithCtags performs preprocessing of the arduino sketch using CTags. func PreprocessSketchWithCtags( sketch *sketch.Sketch, buildPath *paths.Path, includes paths.PathList, - lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool, + lineOffset int, buildProperties *properties.Map, + onlyUpdateCompilationDatabase, verbose bool, ) (Result, error) { // Create a temporary working directory tmpDir, err := paths.MkTempDir("", "") @@ -52,43 +53,43 @@ func PreprocessSketchWithCtags( defer tmpDir.RemoveAll() ctagsTarget := tmpDir.Join("sketch_merged.cpp") - normalOutput := &bytes.Buffer{} - verboseOutput := &bytes.Buffer{} + stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} // Run GCC preprocessor sourceFile := buildPath.Join("sketch", sketch.MainFile.Base()+".cpp") result, err := GCC(sourceFile, ctagsTarget, includes, buildProperties) - verboseOutput.Write(result.Stdout()) - verboseOutput.Write(result.Stderr()) - normalOutput.Write(result.Stderr()) + stdout.Write(result.Stdout()) + stderr.Write(result.Stderr()) if err != nil { if !onlyUpdateCompilationDatabase { - return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err + return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err } // Do not bail out if we are generating the compile commands database - normalOutput.WriteString(fmt.Sprintf("%s: %s", + stderr.WriteString(fmt.Sprintf("%s: %s", tr("An error occurred adding prototypes"), tr("the compilation database may be incomplete or inaccurate"))) if err := sourceFile.CopyTo(ctagsTarget); err != nil { - return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err + return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err } } if src, err := ctagsTarget.ReadFile(); err == nil { filteredSource := filterSketchSource(sketch, bytes.NewReader(src), false) if err := ctagsTarget.WriteFile([]byte(filteredSource)); err != nil { - return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err + return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err } } else { - return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err + return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err } // Run CTags on gcc-preprocessed source ctagsOutput, ctagsStdErr, err := RunCTags(ctagsTarget, buildProperties) - verboseOutput.Write(ctagsStdErr) + if verbose { + stderr.Write(ctagsStdErr) + } if err != nil { - return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err + return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err } // Parse CTags output @@ -103,13 +104,13 @@ func PreprocessSketchWithCtags( if sourceData, err := sourceFile.ReadFile(); err == nil { source = string(sourceData) } else { - return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err + return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err } source = strings.ReplaceAll(source, "\r\n", "\n") source = strings.ReplaceAll(source, "\r", "\n") sourceRows := strings.Split(source, "\n") if isFirstFunctionOutsideOfSource(firstFunctionLine, sourceRows) { - return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, nil + return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, nil } insertionLine := firstFunctionLine + lineOffset - 1 @@ -135,7 +136,7 @@ func PreprocessSketchWithCtags( // Write back arduino-preprocess output to the sourceFile err = sourceFile.WriteFile([]byte(preprocessedSource)) - return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err + return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err } func composePrototypeSection(line int, prototypes []*ctags.Prototype) string { diff --git a/internal/arduino/builder/preprocess_sketch.go b/internal/arduino/builder/preprocess_sketch.go index 75110adaa29..3296cb517c3 100644 --- a/internal/arduino/builder/preprocess_sketch.go +++ b/internal/arduino/builder/preprocess_sketch.go @@ -25,13 +25,12 @@ func (b *Builder) preprocessSketch(includes paths.PathList) error { // In the future we might change the preprocessor result, err := preprocessor.PreprocessSketchWithCtags( b.sketch, b.buildPath, includes, b.lineOffset, - b.buildProperties, b.onlyUpdateCompilationDatabase, + b.buildProperties, b.onlyUpdateCompilationDatabase, b.logger.Verbose(), ) if b.logger.Verbose() { b.logger.WriteStdout(result.Stdout()) - } else { - b.logger.WriteStdout(result.Stderr()) } + b.logger.WriteStdout(result.Stderr()) b.diagnosticStore.Parse(result.Args(), result.Stderr()) return err