Skip to content

Commit 6584638

Browse files
introduce diagnosticmanager instead of using a callback approach
1 parent 7b4f713 commit 6584638

File tree

8 files changed

+83
-41
lines changed

8 files changed

+83
-41
lines changed

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

+12-31
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/compilation"
2727
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/detector"
28+
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/diagnosticmanager"
2829
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/diagnostics"
2930
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/logger"
3031
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/progress"
@@ -36,7 +37,6 @@ import (
3637
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3738
"github.com/arduino/go-paths-helper"
3839
"github.com/arduino/go-properties-orderedmap"
39-
"github.com/sirupsen/logrus"
4040
)
4141

4242
// ErrSketchCannotBeLocatedInBuildPath fixdoc
@@ -94,11 +94,7 @@ type Builder struct {
9494

9595
toolEnv []string
9696

97-
// This is a function used to parse the output of the compiler
98-
// It is used to extract errors and warnings
99-
compilerOutputParser diagnostics.CompilerOutputParserCB
100-
// and here are the diagnostics parsed from the compiler
101-
compilerDiagnostics diagnostics.Diagnostics
97+
diagnosticsManager *diagnosticmanager.Manager
10298
}
10399

104100
// buildArtifacts contains the result of various build
@@ -199,6 +195,7 @@ func NewBuilder(
199195
logger.Warn(string(verboseOut))
200196
}
201197

198+
diagnosticmanager := diagnosticmanager.New()
202199
b := &Builder{
203200
sketch: sk,
204201
buildProperties: buildProperties,
@@ -220,12 +217,6 @@ func NewBuilder(
220217
targetPlatform: targetPlatform,
221218
actualPlatform: actualPlatform,
222219
toolEnv: toolEnv,
223-
libsDetector: detector.NewSketchLibrariesDetector(
224-
libsManager, libsResolver,
225-
useCachedLibrariesResolution,
226-
onlyUpdateCompilationDatabase,
227-
logger,
228-
),
229220
buildOptions: newBuildOptions(
230221
hardwareDirs, otherLibrariesDirs,
231222
builtInLibrariesDirs, buildPath,
@@ -237,25 +228,15 @@ func NewBuilder(
237228
buildProperties.GetPath("runtime.platform.path"),
238229
buildProperties.GetPath("build.core.path"), // TODO can we buildCorePath ?
239230
),
231+
diagnosticsManager: diagnosticmanager,
232+
libsDetector: detector.NewSketchLibrariesDetector(
233+
libsManager, libsResolver,
234+
useCachedLibrariesResolution,
235+
onlyUpdateCompilationDatabase,
236+
logger,
237+
diagnosticmanager,
238+
),
240239
}
241-
242-
b.compilerOutputParser = func(cmdline []string, out []byte) {
243-
compiler := diagnostics.DetectCompilerFromCommandLine(
244-
cmdline,
245-
false, // at the moment compiler-probing is not required
246-
)
247-
if compiler == nil {
248-
logrus.Warnf("Could not detect compiler from: %s", cmdline)
249-
return
250-
}
251-
diags, err := diagnostics.ParseCompilerOutput(compiler, out)
252-
if err != nil {
253-
logrus.Warnf("Error parsing compiler output: %s", err)
254-
return
255-
}
256-
b.compilerDiagnostics = append(b.compilerDiagnostics, diags...)
257-
}
258-
259240
return b, nil
260241
}
261242

@@ -281,7 +262,7 @@ func (b *Builder) ImportedLibraries() libraries.List {
281262

282263
// CompilerDiagnostics returns the parsed compiler diagnostics
283264
func (b *Builder) CompilerDiagnostics() diagnostics.Diagnostics {
284-
return b.compilerDiagnostics
265+
return b.diagnosticsManager.CompilerDiagnostics()
285266
}
286267

287268
// Preprocess fixdoc

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ func (b *Builder) compileFileWithRecipe(
166166
b.logger.WriteStderr(commandStderr.Bytes())
167167

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

174174
// ...and then return the error

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strings"
2727
"time"
2828

29+
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/diagnosticmanager"
2930
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/logger"
3031
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/preprocessor"
3132
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/utils"
@@ -57,6 +58,7 @@ type SketchLibrariesDetector struct {
5758
librariesResolutionResults map[string]libraryResolutionResult
5859
includeFolders paths.PathList
5960
logger *logger.BuilderLogger
61+
diagnosticManager *diagnosticmanager.Manager
6062
}
6163

6264
// NewSketchLibrariesDetector todo
@@ -66,6 +68,7 @@ func NewSketchLibrariesDetector(
6668
useCachedLibrariesResolution bool,
6769
onlyUpdateCompilationDatabase bool,
6870
logger *logger.BuilderLogger,
71+
diagnosticManager *diagnosticmanager.Manager,
6972
) *SketchLibrariesDetector {
7073
return &SketchLibrariesDetector{
7174
librariesManager: lm,
@@ -76,6 +79,7 @@ func NewSketchLibrariesDetector(
7679
includeFolders: paths.PathList{},
7780
onlyUpdateCompilationDatabase: onlyUpdateCompilationDatabase,
7881
logger: logger,
82+
diagnosticManager: diagnosticManager,
7983
}
8084
}
8185

@@ -347,7 +351,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
347351
}
348352
} else {
349353
var preprocStdout []byte
350-
preprocStdout, preprocStderr, preprocErr = preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties)
354+
preprocStdout, preprocStderr, preprocErr = preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties, l.diagnosticManager)
351355
if l.logger.Verbose() {
352356
l.logger.WriteStdout(preprocStdout)
353357
}
@@ -379,7 +383,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
379383
if preprocErr == nil || preprocStderr == nil {
380384
// Filename came from cache, so run preprocessor to obtain error to show
381385
var preprocStdout []byte
382-
preprocStdout, preprocStderr, preprocErr = preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties)
386+
preprocStdout, preprocStderr, preprocErr = preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties, l.diagnosticManager)
383387
if l.logger.Verbose() {
384388
l.logger.WriteStdout(preprocStdout)
385389
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package diagnosticmanager
2+
3+
import (
4+
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/diagnostics"
5+
"github.com/sirupsen/logrus"
6+
)
7+
8+
type Manager struct {
9+
results diagnostics.Diagnostics
10+
lastInsertIndex int
11+
}
12+
13+
func New() *Manager {
14+
return &Manager{lastInsertIndex: -1}
15+
}
16+
17+
func (m *Manager) Parse(cmdline []string, out []byte) {
18+
compiler := diagnostics.DetectCompilerFromCommandLine(
19+
cmdline,
20+
false, // at the moment compiler-probing is not required
21+
)
22+
if compiler == nil {
23+
logrus.Warnf("Could not detect compiler from: %s", cmdline)
24+
return
25+
}
26+
diags, err := diagnostics.ParseCompilerOutput(compiler, out)
27+
if err != nil {
28+
logrus.Warnf("Error parsing compiler output: %s", err)
29+
return
30+
}
31+
m.lastInsertIndex += len(diags)
32+
m.results = append(m.results, diags...)
33+
}
34+
35+
func (m *Manager) CompilerDiagnostics() diagnostics.Diagnostics {
36+
return m.results
37+
}

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

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

25+
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/diagnosticmanager"
2526
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/utils"
2627
"github.com/arduino/arduino-cli/internal/arduino/sketch"
2728
"github.com/arduino/go-paths-helper"
@@ -30,7 +31,11 @@ import (
3031

3132
// PreprocessSketchWithArduinoPreprocessor performs preprocessing of the arduino sketch
3233
// using arduino-preprocessor (https://github.com/arduino/arduino-preprocessor).
33-
func PreprocessSketchWithArduinoPreprocessor(sk *sketch.Sketch, buildPath *paths.Path, includeFolders paths.PathList, lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool) ([]byte, []byte, error) {
34+
func PreprocessSketchWithArduinoPreprocessor(
35+
sk *sketch.Sketch, buildPath *paths.Path, includeFolders paths.PathList,
36+
lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool,
37+
diagnosticmanager *diagnosticmanager.Manager,
38+
) ([]byte, []byte, error) {
3439
verboseOut := &bytes.Buffer{}
3540
normalOut := &bytes.Buffer{}
3641
if err := buildPath.Join("preproc").MkdirAll(); err != nil {
@@ -39,7 +44,7 @@ func PreprocessSketchWithArduinoPreprocessor(sk *sketch.Sketch, buildPath *paths
3944

4045
sourceFile := buildPath.Join("sketch", sk.MainFile.Base()+".cpp")
4146
targetFile := buildPath.Join("preproc", "sketch_merged.cpp")
42-
gccStdout, gccStderr, err := GCC(sourceFile, targetFile, includeFolders, buildProperties)
47+
gccStdout, gccStderr, err := GCC(sourceFile, targetFile, includeFolders, buildProperties, diagnosticmanager)
4348
verboseOut.Write(gccStdout)
4449
verboseOut.Write(gccStderr)
4550
if err != nil {

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ 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"
2930
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/preprocessor/internal/ctags"
3031
"github.com/arduino/arduino-cli/internal/arduino/sketch"
3132
"github.com/arduino/arduino-cli/internal/i18n"
@@ -40,7 +41,11 @@ var tr = i18n.Tr
4041
var DebugPreprocessor bool
4142

4243
// PreprocessSketchWithCtags performs preprocessing of the arduino sketch using CTags.
43-
func PreprocessSketchWithCtags(sketch *sketch.Sketch, buildPath *paths.Path, includes paths.PathList, lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool) ([]byte, []byte, error) {
44+
func PreprocessSketchWithCtags(
45+
sketch *sketch.Sketch, buildPath *paths.Path, includes paths.PathList,
46+
lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool,
47+
diagnosticManager *diagnosticmanager.Manager,
48+
) ([]byte, []byte, error) {
4449
// Create a temporary working directory
4550
tmpDir, err := paths.MkTempDir("", "")
4651
if err != nil {
@@ -54,7 +59,7 @@ func PreprocessSketchWithCtags(sketch *sketch.Sketch, buildPath *paths.Path, inc
5459

5560
// Run GCC preprocessor
5661
sourceFile := buildPath.Join("sketch", sketch.MainFile.Base()+".cpp")
57-
gccStdout, gccStderr, err := GCC(sourceFile, ctagsTarget, includes, buildProperties)
62+
gccStdout, gccStderr, err := GCC(sourceFile, ctagsTarget, includes, buildProperties, diagnosticManager)
5863
verboseOutput.Write(gccStdout)
5964
verboseOutput.Write(gccStderr)
6065
normalOutput.Write(gccStderr)

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@ 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"
2627
"github.com/arduino/go-paths-helper"
2728
"github.com/arduino/go-properties-orderedmap"
2829
)
2930

3031
// GCC performs a run of the gcc preprocess (macro/includes expansion). The function outputs the result
3132
// to targetFilePath. Returns the stdout/stderr of gcc if any.
32-
func GCC(sourceFilePath *paths.Path, targetFilePath *paths.Path, includes paths.PathList, buildProperties *properties.Map) ([]byte, []byte, error) {
33+
func GCC(
34+
sourceFilePath, targetFilePath *paths.Path,
35+
includes paths.PathList, buildProperties *properties.Map,
36+
diagnosticManager *diagnosticmanager.Manager,
37+
) ([]byte, []byte, error) {
3338
gccBuildProperties := properties.NewMap()
3439
gccBuildProperties.Set("preproc.macros.flags", "-w -x c++ -E -CC")
3540
gccBuildProperties.Merge(buildProperties)
@@ -74,6 +79,10 @@ func GCC(sourceFilePath *paths.Path, targetFilePath *paths.Path, includes paths.
7479
}
7580
stdout, stderr, err := proc.RunAndCaptureOutput(context.Background())
7681

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

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

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func (b *Builder) preprocessSketch(includes paths.PathList) error {
2626
normalOutput, verboseOutput, err := preprocessor.PreprocessSketchWithCtags(
2727
b.sketch, b.buildPath, includes, b.lineOffset,
2828
b.buildProperties, b.onlyUpdateCompilationDatabase,
29+
b.diagnosticsManager,
2930
)
3031
if b.logger.Verbose() {
3132
b.logger.WriteStdout(verboseOutput)

0 commit comments

Comments
 (0)