Skip to content

Commit

Permalink
polish the stdout/stderr assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Feb 6, 2024
1 parent 1f6ce27 commit 7c81dbf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
28 changes: 13 additions & 15 deletions internal/arduino/builder/internal/preprocessor/ctags.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,43 +52,41 @@ 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)
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
Expand All @@ -103,13 +101,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
Expand All @@ -135,7 +133,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 {
Expand Down
3 changes: 1 addition & 2 deletions internal/arduino/builder/preprocess_sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ func (b *Builder) preprocessSketch(includes paths.PathList) error {
)
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
Expand Down

0 comments on commit 7c81dbf

Please sign in to comment.