diff --git a/internal/arduino/builder/internal/preprocessor/arduino_preprocessor.go b/internal/arduino/builder/internal/preprocessor/arduino_preprocessor.go index e991a6f0240..da8cde2adfc 100644 --- a/internal/arduino/builder/internal/preprocessor/arduino_preprocessor.go +++ b/internal/arduino/builder/internal/preprocessor/arduino_preprocessor.go @@ -33,11 +33,11 @@ import ( func PreprocessSketchWithArduinoPreprocessor( sk *sketch.Sketch, buildPath *paths.Path, includeFolders paths.PathList, lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool, -) (Result, error) { +) (*Result, error) { verboseOut := &bytes.Buffer{} normalOut := &bytes.Buffer{} if err := buildPath.Join("preproc").MkdirAll(); err != nil { - return Result{}, err + return nil, err } sourceFile := buildPath.Join("sketch", sk.MainFile.Base()+".cpp") @@ -46,7 +46,7 @@ func PreprocessSketchWithArduinoPreprocessor( verboseOut.Write(gccResult.Stdout()) verboseOut.Write(gccResult.Stderr()) if err != nil { - return Result{}, err + return nil, err } arduiniPreprocessorProperties := properties.NewMap() @@ -59,18 +59,18 @@ func PreprocessSketchWithArduinoPreprocessor( arduiniPreprocessorProperties.SetPath("source_file", targetFile) pattern := arduiniPreprocessorProperties.Get("pattern") if pattern == "" { - return Result{}, errors.New(tr("arduino-preprocessor pattern is missing")) + return nil, errors.New(tr("arduino-preprocessor pattern is missing")) } commandLine := arduiniPreprocessorProperties.ExpandPropsInString(pattern) parts, err := properties.SplitQuotedString(commandLine, `"'`, false) if err != nil { - return Result{}, err + return nil, err } command, err := paths.NewProcess(nil, parts...) if err != nil { - return Result{}, err + return nil, err } if runtime.GOOS == "windows" { // chdir in the uppermost directory to avoid UTF-8 bug in clang (https://github.com/arduino/arduino-preprocessor/issues/2) @@ -81,13 +81,13 @@ func PreprocessSketchWithArduinoPreprocessor( commandStdOut, commandStdErr, err := command.RunAndCaptureOutput(context.Background()) verboseOut.Write(commandStdErr) if err != nil { - return Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err + return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err } result := utils.NormalizeUTF8(commandStdOut) destFile := buildPath.Join(sk.MainFile.Base() + ".cpp") if err := destFile.WriteFile(result); err != nil { - return Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err + return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err } - return Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err + return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err } diff --git a/internal/arduino/builder/internal/preprocessor/ctags.go b/internal/arduino/builder/internal/preprocessor/ctags.go index 5ddbbecde52..c9484593c74 100644 --- a/internal/arduino/builder/internal/preprocessor/ctags.go +++ b/internal/arduino/builder/internal/preprocessor/ctags.go @@ -43,11 +43,11 @@ var DebugPreprocessor bool func PreprocessSketchWithCtags( sketch *sketch.Sketch, buildPath *paths.Path, includes paths.PathList, lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool, -) (Result, error) { +) (*Result, error) { // Create a temporary working directory tmpDir, err := paths.MkTempDir("", "") if err != nil { - return Result{}, err + return nil, err } defer tmpDir.RemoveAll() ctagsTarget := tmpDir.Join("sketch_merged.cpp") @@ -61,7 +61,7 @@ func PreprocessSketchWithCtags( stderr.Write(result.Stderr()) if err != nil { if !onlyUpdateCompilationDatabase { - return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.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 @@ -69,24 +69,24 @@ func PreprocessSketchWithCtags( 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: stdout.Bytes(), stderr: stderr.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: stdout.Bytes(), stderr: stderr.Bytes()}, err + return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err } } else { - return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.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) stderr.Write(ctagsStdErr) if err != nil { - return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err + return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err } // Parse CTags output @@ -101,13 +101,13 @@ func PreprocessSketchWithCtags( if sourceData, err := sourceFile.ReadFile(); err == nil { source = string(sourceData) } else { - return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.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: stdout.Bytes(), stderr: stderr.Bytes()}, nil + return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, nil } insertionLine := firstFunctionLine + lineOffset - 1 @@ -133,7 +133,7 @@ func PreprocessSketchWithCtags( // Write back arduino-preprocess output to the sourceFile err = sourceFile.WriteFile([]byte(preprocessedSource)) - return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.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 975861c135b..4be17eaaf47 100644 --- a/internal/arduino/builder/preprocess_sketch.go +++ b/internal/arduino/builder/preprocess_sketch.go @@ -27,11 +27,13 @@ func (b *Builder) preprocessSketch(includes paths.PathList) error { b.sketch, b.buildPath, includes, b.lineOffset, b.buildProperties, b.onlyUpdateCompilationDatabase, ) - if b.logger.Verbose() { - b.logger.WriteStdout(result.Stdout()) + if result != nil { + if b.logger.Verbose() { + b.logger.WriteStdout(result.Stdout()) + } + b.logger.WriteStdout(result.Stderr()) + b.diagnosticStore.Parse(result.Args(), result.Stderr()) } - b.logger.WriteStdout(result.Stderr()) - b.diagnosticStore.Parse(result.Args(), result.Stderr()) return err }