Skip to content

Commit 0014ecd

Browse files
add diagnsotics to preprocessing step
1 parent 205a6a5 commit 0014ecd

File tree

6 files changed

+52
-14
lines changed

6 files changed

+52
-14
lines changed

internal/arduino/builder/builder.go

+21-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io"
2222
"os"
2323
"path/filepath"
24+
"slices"
2425
"strings"
2526

2627
"github.com/arduino/arduino-cli/internal/arduino/builder/internal/compilation"
@@ -220,12 +221,6 @@ func NewBuilder(
220221
targetPlatform: targetPlatform,
221222
actualPlatform: actualPlatform,
222223
toolEnv: toolEnv,
223-
libsDetector: detector.NewSketchLibrariesDetector(
224-
libsManager, libsResolver,
225-
useCachedLibrariesResolution,
226-
onlyUpdateCompilationDatabase,
227-
logger,
228-
),
229224
buildOptions: newBuildOptions(
230225
hardwareDirs, otherLibrariesDirs,
231226
builtInLibrariesDirs, buildPath,
@@ -256,6 +251,14 @@ func NewBuilder(
256251
b.compilerDiagnostics = append(b.compilerDiagnostics, diags...)
257252
}
258253

254+
b.libsDetector = detector.NewSketchLibrariesDetector(
255+
libsManager, libsResolver,
256+
useCachedLibrariesResolution,
257+
onlyUpdateCompilationDatabase,
258+
logger,
259+
b.compilerOutputParser,
260+
)
261+
259262
return b, nil
260263
}
261264

@@ -281,7 +284,18 @@ func (b *Builder) ImportedLibraries() libraries.List {
281284

282285
// CompilerDiagnostics returns the parsed compiler diagnostics
283286
func (b *Builder) CompilerDiagnostics() diagnostics.Diagnostics {
284-
return b.compilerDiagnostics
287+
// When producing the preprocessing diagnostics, we might have duplicates
288+
// caused by the run of the libraries detector and the sketch preprocessing.
289+
return slices.CompactFunc(b.compilerDiagnostics, func(d1, d2 *diagnostics.Diagnostic) bool {
290+
if d1 == nil || d2 == nil {
291+
return false
292+
}
293+
return d1.Column == d2.Column &&
294+
d1.File == d2.File &&
295+
d1.Line == d2.Line &&
296+
d1.Message == d2.Message &&
297+
d1.Severity == d2.Severity
298+
})
285299
}
286300

287301
// Preprocess fixdoc

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/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+
compilerOutputParser diagnostics.CompilerOutputParserCB
6062
}
6163

6264
// NewSketchLibrariesDetector todo
@@ -66,6 +68,7 @@ func NewSketchLibrariesDetector(
6668
useCachedLibrariesResolution bool,
6769
onlyUpdateCompilationDatabase bool,
6870
logger *logger.BuilderLogger,
71+
compilerOutputParser diagnostics.CompilerOutputParserCB,
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+
compilerOutputParser: compilerOutputParser,
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, nil)
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.compilerOutputParser)
383387
if l.logger.Verbose() {
384388
l.logger.WriteStdout(preprocStdout)
385389
}

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/diagnostics"
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+
compilerOutputParserCB diagnostics.CompilerOutputParserCB,
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, compilerOutputParserCB)
4348
verboseOut.Write(gccStdout)
4449
verboseOut.Write(gccStderr)
4550
if err != nil {

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/diagnostics"
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+
compilerOutputParserCB diagnostics.CompilerOutputParserCB,
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, compilerOutputParserCB)
5863
verboseOutput.Write(gccStdout)
5964
verboseOutput.Write(gccStderr)
6065
normalOutput.Write(gccStderr)

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/diagnostics"
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+
compilerOutputParserCB diagnostics.CompilerOutputParserCB,
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 compilerOutputParserCB != nil {
83+
compilerOutputParserCB(proc.GetArgs(), stderr)
84+
}
85+
7786
// Append gcc arguments to stdout
7887
stdout = append([]byte(fmt.Sprintln(strings.Join(args, " "))), stdout...)
7988

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.compilerOutputParser,
2930
)
3031
if b.logger.Verbose() {
3132
b.logger.WriteStdout(verboseOutput)

0 commit comments

Comments
 (0)