Skip to content

Commit 5bc4e01

Browse files
polish the stdout/stderr assignments
1 parent 77141bf commit 5bc4e01

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

Diff for: internal/arduino/builder/internal/preprocessor/ctags.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ var DebugPreprocessor bool
4242
// PreprocessSketchWithCtags performs preprocessing of the arduino sketch using CTags.
4343
func PreprocessSketchWithCtags(
4444
sketch *sketch.Sketch, buildPath *paths.Path, includes paths.PathList,
45-
lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool,
45+
lineOffset int, buildProperties *properties.Map,
46+
onlyUpdateCompilationDatabase, verbose bool,
4647
) (Result, error) {
4748
// Create a temporary working directory
4849
tmpDir, err := paths.MkTempDir("", "")
@@ -52,43 +53,43 @@ func PreprocessSketchWithCtags(
5253
defer tmpDir.RemoveAll()
5354
ctagsTarget := tmpDir.Join("sketch_merged.cpp")
5455

55-
normalOutput := &bytes.Buffer{}
56-
verboseOutput := &bytes.Buffer{}
56+
stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{}
5757

5858
// Run GCC preprocessor
5959
sourceFile := buildPath.Join("sketch", sketch.MainFile.Base()+".cpp")
6060
result, err := GCC(sourceFile, ctagsTarget, includes, buildProperties)
61-
verboseOutput.Write(result.Stdout())
62-
verboseOutput.Write(result.Stderr())
63-
normalOutput.Write(result.Stderr())
61+
stdout.Write(result.Stdout())
62+
stderr.Write(result.Stderr())
6463
if err != nil {
6564
if !onlyUpdateCompilationDatabase {
66-
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err
65+
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
6766
}
6867

6968
// Do not bail out if we are generating the compile commands database
70-
normalOutput.WriteString(fmt.Sprintf("%s: %s",
69+
stderr.WriteString(fmt.Sprintf("%s: %s",
7170
tr("An error occurred adding prototypes"),
7271
tr("the compilation database may be incomplete or inaccurate")))
7372
if err := sourceFile.CopyTo(ctagsTarget); err != nil {
74-
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err
73+
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
7574
}
7675
}
7776

7877
if src, err := ctagsTarget.ReadFile(); err == nil {
7978
filteredSource := filterSketchSource(sketch, bytes.NewReader(src), false)
8079
if err := ctagsTarget.WriteFile([]byte(filteredSource)); err != nil {
81-
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err
80+
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
8281
}
8382
} else {
84-
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err
83+
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
8584
}
8685

8786
// Run CTags on gcc-preprocessed source
8887
ctagsOutput, ctagsStdErr, err := RunCTags(ctagsTarget, buildProperties)
89-
verboseOutput.Write(ctagsStdErr)
88+
if verbose {
89+
stderr.Write(ctagsStdErr)
90+
}
9091
if err != nil {
91-
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err
92+
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
9293
}
9394

9495
// Parse CTags output
@@ -103,13 +104,13 @@ func PreprocessSketchWithCtags(
103104
if sourceData, err := sourceFile.ReadFile(); err == nil {
104105
source = string(sourceData)
105106
} else {
106-
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err
107+
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
107108
}
108109
source = strings.ReplaceAll(source, "\r\n", "\n")
109110
source = strings.ReplaceAll(source, "\r", "\n")
110111
sourceRows := strings.Split(source, "\n")
111112
if isFirstFunctionOutsideOfSource(firstFunctionLine, sourceRows) {
112-
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, nil
113+
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, nil
113114
}
114115

115116
insertionLine := firstFunctionLine + lineOffset - 1
@@ -135,7 +136,7 @@ func PreprocessSketchWithCtags(
135136

136137
// Write back arduino-preprocess output to the sourceFile
137138
err = sourceFile.WriteFile([]byte(preprocessedSource))
138-
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err
139+
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
139140
}
140141

141142
func composePrototypeSection(line int, prototypes []*ctags.Prototype) string {

Diff for: internal/arduino/builder/preprocess_sketch.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ func (b *Builder) preprocessSketch(includes paths.PathList) error {
2525
// In the future we might change the preprocessor
2626
result, err := preprocessor.PreprocessSketchWithCtags(
2727
b.sketch, b.buildPath, includes, b.lineOffset,
28-
b.buildProperties, b.onlyUpdateCompilationDatabase,
28+
b.buildProperties, b.onlyUpdateCompilationDatabase, b.logger.Verbose(),
2929
)
3030
if b.logger.Verbose() {
3131
b.logger.WriteStdout(result.Stdout())
32-
} else {
33-
b.logger.WriteStdout(result.Stderr())
3432
}
33+
b.logger.WriteStdout(result.Stderr())
3534
b.diagnosticStore.Parse(result.Args(), result.Stderr())
3635

3736
return err

0 commit comments

Comments
 (0)