Skip to content

Commit d99184e

Browse files
Add diagnostics in the preprocessor (arduino#2515)
1 parent a93f755 commit d99184e

File tree

16 files changed

+266
-99
lines changed

16 files changed

+266
-99
lines changed

Diff for: Taskfile.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ tasks:
137137
desc: Add missing go license headers
138138
cmds:
139139
- go install github.com/google/[email protected]
140-
- addlicense -c "ARDUINO SA (http://www.arduino.cc/)" -f ./license_header.tpl **/*.go
140+
- addlicense -c "ARDUINO SA (http://www.arduino.cc/)" -f ./license_header.tpl $(find . -name "*.go" -type f -print0 | xargs -0)
141141

142142
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-markdown-task/Taskfile.yml
143143
markdown:check-links:

Diff for: commands/internal/instances/instances.go

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2024 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
116
package instances
217

318
import (

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

+11-31
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3737
"github.com/arduino/go-paths-helper"
3838
"github.com/arduino/go-properties-orderedmap"
39-
"github.com/sirupsen/logrus"
4039
)
4140

4241
// ErrSketchCannotBeLocatedInBuildPath fixdoc
@@ -94,11 +93,7 @@ type Builder struct {
9493

9594
toolEnv []string
9695

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
96+
diagnosticStore *diagnostics.Store
10297
}
10398

10499
// buildArtifacts contains the result of various build
@@ -199,6 +194,7 @@ func NewBuilder(
199194
logger.Warn(string(verboseOut))
200195
}
201196

197+
diagnosticStore := diagnostics.NewStore()
202198
b := &Builder{
203199
sketch: sk,
204200
buildProperties: buildProperties,
@@ -220,12 +216,6 @@ func NewBuilder(
220216
targetPlatform: targetPlatform,
221217
actualPlatform: actualPlatform,
222218
toolEnv: toolEnv,
223-
libsDetector: detector.NewSketchLibrariesDetector(
224-
libsManager, libsResolver,
225-
useCachedLibrariesResolution,
226-
onlyUpdateCompilationDatabase,
227-
logger,
228-
),
229219
buildOptions: newBuildOptions(
230220
hardwareDirs, otherLibrariesDirs,
231221
builtInLibrariesDirs, buildPath,
@@ -237,25 +227,15 @@ func NewBuilder(
237227
buildProperties.GetPath("runtime.platform.path"),
238228
buildProperties.GetPath("build.core.path"), // TODO can we buildCorePath ?
239229
),
230+
diagnosticStore: diagnosticStore,
231+
libsDetector: detector.NewSketchLibrariesDetector(
232+
libsManager, libsResolver,
233+
useCachedLibrariesResolution,
234+
onlyUpdateCompilationDatabase,
235+
logger,
236+
diagnosticStore,
237+
),
240238
}
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-
259239
return b, nil
260240
}
261241

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

282262
// CompilerDiagnostics returns the parsed compiler diagnostics
283263
func (b *Builder) CompilerDiagnostics() diagnostics.Diagnostics {
284-
return b.compilerDiagnostics
264+
return b.diagnosticStore.Diagnostics()
285265
}
286266

287267
// 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.diagnosticStore != nil {
170+
b.diagnosticStore.Parse(command.GetArgs(), commandStdout.Bytes())
171+
b.diagnosticStore.Parse(command.GetArgs(), commandStderr.Bytes())
172172
}
173173

174174
// ...and then return the error

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

+18-12
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/diagnostics"
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+
diagnosticStore *diagnostics.Store
6062
}
6163

6264
// NewSketchLibrariesDetector todo
@@ -66,6 +68,7 @@ func NewSketchLibrariesDetector(
6668
useCachedLibrariesResolution bool,
6769
onlyUpdateCompilationDatabase bool,
6870
logger *logger.BuilderLogger,
71+
diagnosticStore *diagnostics.Store,
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+
diagnosticStore: diagnosticStore,
7983
}
8084
}
8185

@@ -337,7 +341,7 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
337341
}
338342

339343
var preprocErr error
340-
var preprocStderr []byte
344+
var preprocFirstResult preprocessor.Result
341345

342346
var missingIncludeH string
343347
if unchanged && cache.valid {
@@ -346,21 +350,20 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
346350
l.logger.Info(tr("Using cached library dependencies for file: %[1]s", sourcePath))
347351
}
348352
} else {
349-
var preprocStdout []byte
350-
preprocStdout, preprocStderr, preprocErr = preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties)
353+
preprocFirstResult, preprocErr = preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties)
351354
if l.logger.Verbose() {
352-
l.logger.WriteStdout(preprocStdout)
355+
l.logger.WriteStdout(preprocFirstResult.Stdout())
353356
}
354357
// Unwrap error and see if it is an ExitError.
355358
var exitErr *exec.ExitError
356359
if preprocErr == nil {
357360
// Preprocessor successful, done
358361
missingIncludeH = ""
359-
} else if isExitErr := errors.As(preprocErr, &exitErr); !isExitErr || preprocStderr == nil {
362+
} else if isExitErr := errors.As(preprocErr, &exitErr); !isExitErr || preprocFirstResult.Stderr() == nil {
360363
// Ignore ExitErrors (e.g. gcc returning non-zero status), but bail out on other errors
361364
return preprocErr
362365
} else {
363-
missingIncludeH = IncludesFinderWithRegExp(string(preprocStderr))
366+
missingIncludeH = IncludesFinderWithRegExp(string(preprocFirstResult.Stderr()))
364367
if missingIncludeH == "" && l.logger.Verbose() {
365368
l.logger.Info(tr("Error while detecting libraries included by %[1]s", sourcePath))
366369
}
@@ -376,22 +379,25 @@ func (l *SketchLibrariesDetector) findIncludesUntilDone(
376379
library := l.resolveLibrary(missingIncludeH, platformArch)
377380
if library == nil {
378381
// Library could not be resolved, show error
379-
if preprocErr == nil || preprocStderr == nil {
382+
if preprocErr == nil || preprocFirstResult.Stderr() == nil {
380383
// Filename came from cache, so run preprocessor to obtain error to show
381-
var preprocStdout []byte
382-
preprocStdout, preprocStderr, preprocErr = preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties)
384+
result, err := preprocessor.GCC(sourcePath, targetFilePath, includeFolders, buildProperties)
383385
if l.logger.Verbose() {
384-
l.logger.WriteStdout(preprocStdout)
386+
l.logger.WriteStdout(result.Stdout())
385387
}
386-
if preprocErr == nil {
388+
if err == nil {
387389
// If there is a missing #include in the cache, but running
388390
// gcc does not reproduce that, there is something wrong.
389391
// Returning an error here will cause the cache to be
390392
// deleted, so hopefully the next compilation will succeed.
391393
return errors.New(tr("Internal error in cache"))
392394
}
395+
l.diagnosticStore.Parse(result.Args(), result.Stderr())
396+
l.logger.WriteStderr(result.Stderr())
397+
return err
393398
}
394-
l.logger.WriteStderr(preprocStderr)
399+
l.diagnosticStore.Parse(preprocFirstResult.Args(), preprocFirstResult.Stderr())
400+
l.logger.WriteStderr(preprocFirstResult.Stderr())
395401
return preprocErr
396402
}
397403

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2024 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package diagnostics
17+
18+
import (
19+
"github.com/sirupsen/logrus"
20+
)
21+
22+
type Store struct {
23+
results Diagnostics
24+
}
25+
26+
func NewStore() *Store {
27+
return &Store{}
28+
}
29+
30+
// Parse parses the output coming from a compiler. It then stores the parsed
31+
// diagnostics in a slice of Diagnostic.
32+
func (m *Store) Parse(cmdline []string, out []byte) {
33+
compiler := DetectCompilerFromCommandLine(
34+
cmdline,
35+
false, // at the moment compiler-probing is not required
36+
)
37+
if compiler == nil {
38+
logrus.Warnf("Could not detect compiler from: %s", cmdline)
39+
return
40+
}
41+
diags, err := ParseCompilerOutput(compiler, out)
42+
if err != nil {
43+
logrus.Warnf("Error parsing compiler output: %s", err)
44+
return
45+
}
46+
m.results = append(m.results, diags...)
47+
}
48+
49+
func (m *Store) Diagnostics() Diagnostics {
50+
return m.results
51+
}

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

+15-13
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,23 @@ import (
3030

3131
// PreprocessSketchWithArduinoPreprocessor performs preprocessing of the arduino sketch
3232
// 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) {
33+
func PreprocessSketchWithArduinoPreprocessor(
34+
sk *sketch.Sketch, buildPath *paths.Path, includeFolders paths.PathList,
35+
lineOffset int, buildProperties *properties.Map, onlyUpdateCompilationDatabase bool,
36+
) (*Result, error) {
3437
verboseOut := &bytes.Buffer{}
3538
normalOut := &bytes.Buffer{}
3639
if err := buildPath.Join("preproc").MkdirAll(); err != nil {
37-
return nil, nil, err
40+
return nil, err
3841
}
3942

4043
sourceFile := buildPath.Join("sketch", sk.MainFile.Base()+".cpp")
4144
targetFile := buildPath.Join("preproc", "sketch_merged.cpp")
42-
gccStdout, gccStderr, err := GCC(sourceFile, targetFile, includeFolders, buildProperties)
43-
verboseOut.Write(gccStdout)
44-
verboseOut.Write(gccStderr)
45+
gccResult, err := GCC(sourceFile, targetFile, includeFolders, buildProperties)
46+
verboseOut.Write(gccResult.Stdout())
47+
verboseOut.Write(gccResult.Stderr())
4548
if err != nil {
46-
return nil, nil, err
49+
return nil, err
4750
}
4851

4952
arduiniPreprocessorProperties := properties.NewMap()
@@ -56,18 +59,18 @@ func PreprocessSketchWithArduinoPreprocessor(sk *sketch.Sketch, buildPath *paths
5659
arduiniPreprocessorProperties.SetPath("source_file", targetFile)
5760
pattern := arduiniPreprocessorProperties.Get("pattern")
5861
if pattern == "" {
59-
return nil, nil, errors.New(tr("arduino-preprocessor pattern is missing"))
62+
return nil, errors.New(tr("arduino-preprocessor pattern is missing"))
6063
}
6164

6265
commandLine := arduiniPreprocessorProperties.ExpandPropsInString(pattern)
6366
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
6467
if err != nil {
65-
return nil, nil, err
68+
return nil, err
6669
}
6770

6871
command, err := paths.NewProcess(nil, parts...)
6972
if err != nil {
70-
return nil, nil, err
73+
return nil, err
7174
}
7275
if runtime.GOOS == "windows" {
7376
// chdir in the uppermost directory to avoid UTF-8 bug in clang (https://github.com/arduino/arduino-preprocessor/issues/2)
@@ -78,14 +81,13 @@ func PreprocessSketchWithArduinoPreprocessor(sk *sketch.Sketch, buildPath *paths
7881
commandStdOut, commandStdErr, err := command.RunAndCaptureOutput(context.Background())
7982
verboseOut.Write(commandStdErr)
8083
if err != nil {
81-
return normalOut.Bytes(), verboseOut.Bytes(), err
84+
return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
8285
}
8386
result := utils.NormalizeUTF8(commandStdOut)
8487

8588
destFile := buildPath.Join(sk.MainFile.Base() + ".cpp")
8689
if err := destFile.WriteFile(result); err != nil {
87-
return normalOut.Bytes(), verboseOut.Bytes(), err
90+
return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
8891
}
89-
90-
return normalOut.Bytes(), verboseOut.Bytes(), err
92+
return &Result{args: gccResult.Args(), stdout: verboseOut.Bytes(), stderr: normalOut.Bytes()}, err
9193
}

0 commit comments

Comments
 (0)