Skip to content

Commit e6c58c4

Browse files
introduce the Result struct returnedby the proprocessing funcs
1 parent 6584638 commit e6c58c4

File tree

7 files changed

+70
-60
lines changed

7 files changed

+70
-60
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ func (b *Builder) compileFileWithRecipe(
167167

168168
// Parse the output of the compiler to gather errors and warnings...
169169
if b.diagnosticsManager != nil {
170-
b.diagnosticsManager.ParseOutput(command.GetArgs(), commandStdout.Bytes())
171-
b.diagnosticsManager.ParseOutput(command.GetArgs(), commandStderr.Bytes())
170+
b.diagnosticsManager.Parse(command.GetArgs(), commandStdout.Bytes())
171+
b.diagnosticsManager.Parse(command.GetArgs(), commandStderr.Bytes())
172172
}
173173

174174
// ...and then return the error

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

+14-12
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
341341
}
342342

343343
var preprocErr error
344-
var preprocStderr []byte
344+
var preprocFirstResult preprocessor.Result
345345

346346
var missingIncludeH string
347347
if unchanged && cache.valid {
@@ -350,21 +350,20 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
350350
l.logger.Info(tr("Using cached library dependencies for file: %[1]s", sourcePath))
351351
}
352352
} else {
353-
var preprocStdout []byte
354-
preprocStdout, preprocStderr, preprocErr = preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties, l.diagnosticManager)
353+
preprocFirstResult, preprocErr = preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties)
355354
if l.logger.Verbose() {
356-
l.logger.WriteStdout(preprocStdout)
355+
l.logger.WriteStdout(preprocFirstResult.Stdout())
357356
}
358357
// Unwrap error and see if it is an ExitError.
359358
var exitErr *exec.ExitError
360359
if preprocErr == nil {
361360
// Preprocessor successful, done
362361
missingIncludeH = ""
363-
} else if isExitErr := errors.As(preprocErr, &exitErr); !isExitErr || preprocStderr == nil {
362+
} else if isExitErr := errors.As(preprocErr, &exitErr); !isExitErr || preprocFirstResult.Stderr() == nil {
364363
// Ignore ExitErrors (e.g. gcc returning non-zero status), but bail out on other errors
365364
return preprocErr
366365
} else {
367-
missingIncludeH = IncludesFinderWithRegExp(string(preprocStderr))
366+
missingIncludeH = IncludesFinderWithRegExp(string(preprocFirstResult.Stderr()))
368367
if missingIncludeH == "" && l.logger.Verbose() {
369368
l.logger.Info(tr("Error while detecting libraries included by %[1]s", sourcePath))
370369
}
@@ -380,22 +379,25 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
380379
library := l.resolveLibrary(missingIncludeH, platformArch)
381380
if library == nil {
382381
// Library could not be resolved, show error
383-
if preprocErr == nil || preprocStderr == nil {
382+
if preprocErr == nil || preprocFirstResult.Stderr() == nil {
384383
// Filename came from cache, so run preprocessor to obtain error to show
385-
var preprocStdout []byte
386-
preprocStdout, preprocStderr, preprocErr = preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties, l.diagnosticManager)
384+
result, err := preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties)
387385
if l.logger.Verbose() {
388-
l.logger.WriteStdout(preprocStdout)
386+
l.logger.WriteStdout(result.Stdout())
389387
}
390-
if preprocErr == nil {
388+
if err == nil {
391389
// If there is a missing #include in the cache, but running
392390
// gcc does not reproduce that, there is something wrong.
393391
// Returning an error here will cause the cache to be
394392
// deleted, so hopefully the next compilation will succeed.
395393
return errors.New(tr("Internal error in cache"))
396394
}
395+
l.diagnosticManager.Parse(result.Args(), result.Stderr())
396+
l.logger.WriteStderr(result.Stderr())
397+
return err
397398
}
398-
l.logger.WriteStderr(preprocStderr)
399+
l.diagnosticManager.Parse(preprocFirstResult.Args(), preprocFirstResult.Stderr())
400+
l.logger.WriteStderr(preprocFirstResult.Stderr())
399401
return preprocErr
400402
}
401403

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

+12-15
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"path/filepath"
2323
"runtime"
2424

25-
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/diagnosticmanager"
2625
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/utils"
2726
"github.com/arduino/arduino-cli/internal/arduino/sketch"
2827
"github.com/arduino/go-paths-helper"
@@ -34,21 +33,20 @@ import (
3433
func PreprocessSketchWithArduinoPreprocessor(
3534
sk *sketch.Sketch, buildPath *paths.Path, includeFolders paths.PathList,
3635
lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool,
37-
diagnosticmanager *diagnosticmanager.Manager,
38-
) ([]byte, []byte, error) {
36+
) (Result, error) {
3937
verboseOut := &bytes.Buffer{}
4038
normalOut := &bytes.Buffer{}
4139
if err := buildPath.Join("preproc").MkdirAll(); err != nil {
42-
return nil, nil, err
40+
return Result{}, err
4341
}
4442

4543
sourceFile := buildPath.Join("sketch", sk.MainFile.Base()+".cpp")
4644
targetFile := buildPath.Join("preproc", "sketch_merged.cpp")
47-
gccStdout, gccStderr, err := GCC(sourceFile, targetFile, includeFolders, buildProperties, diagnosticmanager)
48-
verboseOut.Write(gccStdout)
49-
verboseOut.Write(gccStderr)
45+
gccResult, err := GCC(sourceFile, targetFile, includeFolders, buildProperties)
46+
verboseOut.Write(gccResult.Stdout())
47+
verboseOut.Write(gccResult.Stderr())
5048
if err != nil {
51-
return nil, nil, err
49+
return Result{}, err
5250
}
5351

5452
arduiniPreprocessorProperties := properties.NewMap()
@@ -61,18 +59,18 @@ func PreprocessSketchWithArduinoPreprocessor(
6159
arduiniPreprocessorProperties.SetPath("source_file", targetFile)
6260
pattern := arduiniPreprocessorProperties.Get("pattern")
6361
if pattern == "" {
64-
return nil, nil, errors.New(tr("arduino-preprocessor pattern is missing"))
62+
return Result{}, errors.New(tr("arduino-preprocessor pattern is missing"))
6563
}
6664

6765
commandLine := arduiniPreprocessorProperties.ExpandPropsInString(pattern)
6866
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
6967
if err != nil {
70-
return nil, nil, err
68+
return Result{}, err
7169
}
7270

7371
command, err := paths.NewProcess(nil, parts...)
7472
if err != nil {
75-
return nil, nil, err
73+
return Result{}, err
7674
}
7775
if runtime.GOOS == "windows" {
7876
// chdir in the uppermost directory to avoid UTF-8 bug in clang (https://github.com/arduino/arduino-preprocessor/issues/2)
@@ -83,14 +81,13 @@ func PreprocessSketchWithArduinoPreprocessor(
8381
commandStdOut, commandStdErr, err := command.RunAndCaptureOutput(context.Background())
8482
verboseOut.Write(commandStdErr)
8583
if err != nil {
86-
return normalOut.Bytes(), verboseOut.Bytes(), err
84+
return Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
8785
}
8886
result := utils.NormalizeUTF8(commandStdOut)
8987

9088
destFile := buildPath.Join(sk.MainFile.Base() + ".cpp")
9189
if err := destFile.WriteFile(result); err != nil {
92-
return normalOut.Bytes(), verboseOut.Bytes(), err
90+
return Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
9391
}
94-
95-
return normalOut.Bytes(), verboseOut.Bytes(), err
92+
return Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
9693
}

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

+14-16
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"strings"
2727

2828
"github.com/arduino/arduino-cli/internal/arduino/builder/cpp"
29-
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/diagnosticmanager"
3029
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/preprocessor/internal/ctags"
3130
"github.com/arduino/arduino-cli/internal/arduino/sketch"
3231
"github.com/arduino/arduino-cli/internal/i18n"
@@ -44,12 +43,11 @@ var DebugPreprocessor bool
4443
func PreprocessSketchWithCtags(
4544
sketch *sketch.Sketch, buildPath *paths.Path, includes paths.PathList,
4645
lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool,
47-
diagnosticManager *diagnosticmanager.Manager,
48-
) ([]byte, []byte, error) {
46+
) (Result, error) {
4947
// Create a temporary working directory
5048
tmpDir, err := paths.MkTempDir("", "")
5149
if err != nil {
52-
return nil, nil, err
50+
return Result{}, err
5351
}
5452
defer tmpDir.RemoveAll()
5553
ctagsTarget := tmpDir.Join("sketch_merged.cpp")
@@ -59,38 +57,38 @@ func PreprocessSketchWithCtags(
5957

6058
// Run GCC preprocessor
6159
sourceFile := buildPath.Join("sketch", sketch.MainFile.Base()+".cpp")
62-
gccStdout, gccStderr, err := GCC(sourceFile, ctagsTarget, includes, buildProperties, diagnosticManager)
63-
verboseOutput.Write(gccStdout)
64-
verboseOutput.Write(gccStderr)
65-
normalOutput.Write(gccStderr)
60+
result, err := GCC(sourceFile, ctagsTarget, includes, buildProperties)
61+
verboseOutput.Write(result.Stdout())
62+
verboseOutput.Write(result.Stderr())
63+
normalOutput.Write(result.Stderr())
6664
if err != nil {
6765
if !onlyUpdateCompilationDatabase {
68-
return normalOutput.Bytes(), verboseOutput.Bytes(), err
66+
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err
6967
}
7068

7169
// Do not bail out if we are generating the compile commands database
7270
normalOutput.WriteString(fmt.Sprintf("%s: %s",
7371
tr("An error occurred adding prototypes"),
7472
tr("the compilation database may be incomplete or inaccurate")))
7573
if err := sourceFile.CopyTo(ctagsTarget); err != nil {
76-
return normalOutput.Bytes(), verboseOutput.Bytes(), err
74+
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err
7775
}
7876
}
7977

8078
if src, err := ctagsTarget.ReadFile(); err == nil {
8179
filteredSource := filterSketchSource(sketch, bytes.NewReader(src), false)
8280
if err := ctagsTarget.WriteFile([]byte(filteredSource)); err != nil {
83-
return normalOutput.Bytes(), verboseOutput.Bytes(), err
81+
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err
8482
}
8583
} else {
86-
return normalOutput.Bytes(), verboseOutput.Bytes(), err
84+
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err
8785
}
8886

8987
// Run CTags on gcc-preprocessed source
9088
ctagsOutput, ctagsStdErr, err := RunCTags(ctagsTarget, buildProperties)
9189
verboseOutput.Write(ctagsStdErr)
9290
if err != nil {
93-
return normalOutput.Bytes(), verboseOutput.Bytes(), err
91+
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err
9492
}
9593

9694
// Parse CTags output
@@ -105,13 +103,13 @@ func PreprocessSketchWithCtags(
105103
if sourceData, err := sourceFile.ReadFile(); err == nil {
106104
source = string(sourceData)
107105
} else {
108-
return normalOutput.Bytes(), verboseOutput.Bytes(), err
106+
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err
109107
}
110108
source = strings.ReplaceAll(source, "\r\n", "\n")
111109
source = strings.ReplaceAll(source, "\r", "\n")
112110
sourceRows := strings.Split(source, "\n")
113111
if isFirstFunctionOutsideOfSource(firstFunctionLine, sourceRows) {
114-
return normalOutput.Bytes(), verboseOutput.Bytes(), nil
112+
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, nil
115113
}
116114

117115
insertionLine := firstFunctionLine + lineOffset - 1
@@ -137,7 +135,7 @@ func PreprocessSketchWithCtags(
137135

138136
// Write back arduino-preprocess output to the sourceFile
139137
err = sourceFile.WriteFile([]byte(preprocessedSource))
140-
return normalOutput.Bytes(), verboseOutput.Bytes(), err
138+
return Result{args: result.Args(), stdout: verboseOutput.Bytes(), stderr: normalOutput.Bytes()}, err
141139
}
142140

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

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

+5-11
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
f "github.com/arduino/arduino-cli/internal/algorithms"
2525
"github.com/arduino/arduino-cli/internal/arduino/builder/cpp"
26-
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/diagnosticmanager"
2726
"github.com/arduino/go-paths-helper"
2827
"github.com/arduino/go-properties-orderedmap"
2928
)
@@ -33,8 +32,7 @@ import (
3332
func GCC(
3433
sourceFilePath, targetFilePath *paths.Path,
3534
includes paths.PathList, buildProperties *properties.Map,
36-
diagnosticManager *diagnosticmanager.Manager,
37-
) ([]byte, []byte, error) {
35+
) (Result, error) {
3836
gccBuildProperties := properties.NewMap()
3937
gccBuildProperties.Set("preproc.macros.flags", "-w -x c++ -E -CC")
4038
gccBuildProperties.Merge(buildProperties)
@@ -59,14 +57,14 @@ func GCC(
5957

6058
pattern := gccBuildProperties.Get(gccPreprocRecipeProperty)
6159
if pattern == "" {
62-
return nil, nil, errors.New(tr("%s pattern is missing", gccPreprocRecipeProperty))
60+
return Result{}, errors.New(tr("%s pattern is missing", gccPreprocRecipeProperty))
6361
}
6462

6563
commandLine := gccBuildProperties.ExpandPropsInString(pattern)
6664
commandLine = properties.DeleteUnexpandedPropsFromString(commandLine)
6765
args, err := properties.SplitQuotedString(commandLine, `"'`, false)
6866
if err != nil {
69-
return nil, nil, err
67+
return Result{}, err
7068
}
7169

7270
// Remove -MMD argument if present. Leaving it will make gcc try
@@ -75,16 +73,12 @@ func GCC(
7573

7674
proc, err := paths.NewProcess(nil, args...)
7775
if err != nil {
78-
return nil, nil, err
76+
return Result{}, err
7977
}
8078
stdout, stderr, err := proc.RunAndCaptureOutput(context.Background())
8179

82-
if diagnosticManager != nil {
83-
diagnosticManager.ParseOutput(proc.GetArgs(), stderr)
84-
}
85-
8680
// Append gcc arguments to stdout
8781
stdout = append([]byte(fmt.Sprintln(strings.Join(args, " "))), stdout...)
8882

89-
return stdout, stderr, err
83+
return Result{args: proc.GetArgs(), stdout: stdout, stderr: stderr}, err
9084
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package preprocessor
2+
3+
type Result struct {
4+
args []string
5+
stdout []byte
6+
stderr []byte
7+
}
8+
9+
func (r Result) Args() []string {
10+
return r.args
11+
}
12+
13+
func (r Result) Stdout() []byte {
14+
return r.stdout
15+
}
16+
17+
func (r Result) Stderr() []byte {
18+
return r.stderr
19+
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ import (
2323
// preprocessSketch fixdoc
2424
func (b *Builder) preprocessSketch(includes paths.PathList) error {
2525
// In the future we might change the preprocessor
26-
normalOutput, verboseOutput, err := preprocessor.PreprocessSketchWithCtags(
26+
result, err := preprocessor.PreprocessSketchWithCtags(
2727
b.sketch, b.buildPath, includes, b.lineOffset,
2828
b.buildProperties, b.onlyUpdateCompilationDatabase,
29-
b.diagnosticsManager,
3029
)
3130
if b.logger.Verbose() {
32-
b.logger.WriteStdout(verboseOutput)
31+
b.logger.WriteStdout(result.Stdout())
3332
} else {
34-
b.logger.WriteStdout(normalOutput)
33+
b.logger.WriteStdout(result.Stderr())
3534
}
35+
b.diagnosticsManager.Parse(result.Args(), result.Stderr())
3636

3737
return err
3838
}

0 commit comments

Comments
 (0)