Skip to content

Commit be0b8c4

Browse files
return pointer to Result
1 parent 5bc4e01 commit be0b8c4

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ import (
3333
func PreprocessSketchWithArduinoPreprocessor(
3434
sk *sketch.Sketch, buildPath *paths.Path, includeFolders paths.PathList,
3535
lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool,
36-
) (Result, error) {
36+
) (*Result, error) {
3737
verboseOut := &bytes.Buffer{}
3838
normalOut := &bytes.Buffer{}
3939
if err := buildPath.Join("preproc").MkdirAll(); err != nil {
40-
return Result{}, err
40+
return nil, err
4141
}
4242

4343
sourceFile := buildPath.Join("sketch", sk.MainFile.Base()+".cpp")
@@ -46,7 +46,7 @@ func PreprocessSketchWithArduinoPreprocessor(
4646
verboseOut.Write(gccResult.Stdout())
4747
verboseOut.Write(gccResult.Stderr())
4848
if err != nil {
49-
return Result{}, err
49+
return nil, err
5050
}
5151

5252
arduiniPreprocessorProperties := properties.NewMap()
@@ -59,18 +59,18 @@ func PreprocessSketchWithArduinoPreprocessor(
5959
arduiniPreprocessorProperties.SetPath("source_file", targetFile)
6060
pattern := arduiniPreprocessorProperties.Get("pattern")
6161
if pattern == "" {
62-
return Result{}, errors.New(tr("arduino-preprocessor pattern is missing"))
62+
return nil, errors.New(tr("arduino-preprocessor pattern is missing"))
6363
}
6464

6565
commandLine := arduiniPreprocessorProperties.ExpandPropsInString(pattern)
6666
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
6767
if err != nil {
68-
return Result{}, err
68+
return nil, err
6969
}
7070

7171
command, err := paths.NewProcess(nil, parts...)
7272
if err != nil {
73-
return Result{}, err
73+
return nil, err
7474
}
7575
if runtime.GOOS == "windows" {
7676
// 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(
8181
commandStdOut, commandStdErr, err := command.RunAndCaptureOutput(context.Background())
8282
verboseOut.Write(commandStdErr)
8383
if err != nil {
84-
return Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
84+
return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
8585
}
8686
result := utils.NormalizeUTF8(commandStdOut)
8787

8888
destFile := buildPath.Join(sk.MainFile.Base() + ".cpp")
8989
if err := destFile.WriteFile(result); err != nil {
90-
return Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
90+
return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
9191
}
92-
return Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
92+
return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
9393
}

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ func PreprocessSketchWithCtags(
4444
sketch *sketch.Sketch, buildPath *paths.Path, includes paths.PathList,
4545
lineOffset int, buildProperties *properties.Map,
4646
onlyUpdateCompilationDatabase, verbose bool,
47-
) (Result, error) {
47+
) (*Result, error) {
4848
// Create a temporary working directory
4949
tmpDir, err := paths.MkTempDir("", "")
5050
if err != nil {
51-
return Result{}, err
51+
return nil, err
5252
}
5353
defer tmpDir.RemoveAll()
5454
ctagsTarget := tmpDir.Join("sketch_merged.cpp")
@@ -62,25 +62,25 @@ func PreprocessSketchWithCtags(
6262
stderr.Write(result.Stderr())
6363
if err != nil {
6464
if !onlyUpdateCompilationDatabase {
65-
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
65+
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
6666
}
6767

6868
// Do not bail out if we are generating the compile commands database
6969
stderr.WriteString(fmt.Sprintf("%s: %s",
7070
tr("An error occurred adding prototypes"),
7171
tr("the compilation database may be incomplete or inaccurate")))
7272
if err := sourceFile.CopyTo(ctagsTarget); err != nil {
73-
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
73+
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
7474
}
7575
}
7676

7777
if src, err := ctagsTarget.ReadFile(); err == nil {
7878
filteredSource := filterSketchSource(sketch, bytes.NewReader(src), false)
7979
if err := ctagsTarget.WriteFile([]byte(filteredSource)); err != nil {
80-
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
80+
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
8181
}
8282
} else {
83-
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
83+
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
8484
}
8585

8686
// Run CTags on gcc-preprocessed source
@@ -89,7 +89,7 @@ func PreprocessSketchWithCtags(
8989
stderr.Write(ctagsStdErr)
9090
}
9191
if err != nil {
92-
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
92+
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
9393
}
9494

9595
// Parse CTags output
@@ -104,13 +104,13 @@ func PreprocessSketchWithCtags(
104104
if sourceData, err := sourceFile.ReadFile(); err == nil {
105105
source = string(sourceData)
106106
} else {
107-
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
107+
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
108108
}
109109
source = strings.ReplaceAll(source, "\r\n", "\n")
110110
source = strings.ReplaceAll(source, "\r", "\n")
111111
sourceRows := strings.Split(source, "\n")
112112
if isFirstFunctionOutsideOfSource(firstFunctionLine, sourceRows) {
113-
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, nil
113+
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, nil
114114
}
115115

116116
insertionLine := firstFunctionLine + lineOffset - 1
@@ -136,7 +136,7 @@ func PreprocessSketchWithCtags(
136136

137137
// Write back arduino-preprocess output to the sourceFile
138138
err = sourceFile.WriteFile([]byte(preprocessedSource))
139-
return Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
139+
return &Result{args: result.Args(), stdout: stdout.Bytes(), stderr: stderr.Bytes()}, err
140140
}
141141

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

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ func (b *Builder) preprocessSketch(includes paths.PathList) error {
2727
b.sketch, b.buildPath, includes, b.lineOffset,
2828
b.buildProperties, b.onlyUpdateCompilationDatabase, b.logger.Verbose(),
2929
)
30-
if b.logger.Verbose() {
31-
b.logger.WriteStdout(result.Stdout())
30+
if result != nil {
31+
if b.logger.Verbose() {
32+
b.logger.WriteStdout(result.Stdout())
33+
}
34+
b.logger.WriteStdout(result.Stderr())
35+
b.diagnosticStore.Parse(result.Args(), result.Stderr())
3236
}
33-
b.logger.WriteStdout(result.Stderr())
34-
b.diagnosticStore.Parse(result.Args(), result.Stderr())
3537

3638
return err
3739
}

0 commit comments

Comments
 (0)