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 f9e4100
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
33 changes: 17 additions & 16 deletions internal/arduino/builder/internal/preprocessor/ctags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("", "")
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions internal/arduino/builder/preprocess_sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f9e4100

Please sign in to comment.