Skip to content

Commit 9ce2904

Browse files
authoredMay 31, 2023
[skip-changelog] legacy: Arduino preprocess subroutine refactorization (part 3) (#2193)
* Converted CTagsRunner into a function * Removed useless tasks from ctags_runner test * Simplified ctags_runner test * Removed some ctags related fields from builder context The last piece in RunCTags: parser := &ctags.CTagsParser{} parser.Parse(ctagsStdout, sketch.MainFile) parser.FixCLinkageTagsDeclarations() prototypes, line := parser.GeneratePrototypes() if line != -1 { prototypesLineWhereToInsert = line } has been moved at the beginning of PrototypesAdder. RunCTags now returns the output of ctags instead of `prototypes` and `line`. This also allows to remove the context variables that keeps those information. * Simplified RunCTags / factored test subroutine * Removed DebugPreprocessor from builder ctx * Added executils.RunAndCaptureOutput * Moved RunCTags out of legacy package
1 parent 74dd907 commit 9ce2904

File tree

9 files changed

+151
-229
lines changed

9 files changed

+151
-229
lines changed
 

‎arduino/builder/preprocessor/ctags.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2023 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 license@arduino.cc.
15+
16+
package preprocessor
17+
18+
import (
19+
"context"
20+
"fmt"
21+
"strings"
22+
23+
"github.com/arduino/arduino-cli/executils"
24+
"github.com/arduino/arduino-cli/i18n"
25+
"github.com/arduino/go-paths-helper"
26+
"github.com/arduino/go-properties-orderedmap"
27+
"github.com/pkg/errors"
28+
)
29+
30+
var tr = i18n.Tr
31+
32+
// RunCTags performs a run of ctags on the given source file. Returns the ctags output and the stderr contents.
33+
func RunCTags(sourceFile *paths.Path, buildProperties *properties.Map) ([]byte, []byte, error) {
34+
ctagsBuildProperties := properties.NewMap()
35+
ctagsBuildProperties.Set("tools.ctags.path", "{runtime.tools.ctags.path}")
36+
ctagsBuildProperties.Set("tools.ctags.cmd.path", "{path}/ctags")
37+
ctagsBuildProperties.Set("tools.ctags.pattern", `"{cmd.path}" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "{source_file}"`)
38+
ctagsBuildProperties.Merge(buildProperties)
39+
ctagsBuildProperties.Merge(ctagsBuildProperties.SubTree("tools").SubTree("ctags"))
40+
ctagsBuildProperties.SetPath("source_file", sourceFile)
41+
42+
pattern := ctagsBuildProperties.Get("pattern")
43+
if pattern == "" {
44+
return nil, nil, errors.Errorf(tr("%s pattern is missing"), "ctags")
45+
}
46+
47+
commandLine := ctagsBuildProperties.ExpandPropsInString(pattern)
48+
parts, err := properties.SplitQuotedString(commandLine, `"'`, false)
49+
if err != nil {
50+
return nil, nil, err
51+
}
52+
proc, err := executils.NewProcess(nil, parts...)
53+
if err != nil {
54+
return nil, nil, err
55+
}
56+
stdout, stderr, err := proc.RunAndCaptureOutput(context.Background())
57+
58+
// Append ctags arguments to stderr
59+
args := fmt.Sprintln(strings.Join(parts, " "))
60+
stderr = append([]byte(args), stderr...)
61+
return stdout, stderr, err
62+
}

‎executils/process.go

+13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package executils
1717

1818
import (
19+
"bytes"
1920
"context"
2021
"io"
2122
"os"
@@ -174,3 +175,15 @@ func (p *Process) RunWithinContext(ctx context.Context) error {
174175
}()
175176
return p.Wait()
176177
}
178+
179+
// RunAndCaptureOutput starts the specified command and waits for it to complete. If the given context
180+
// is canceled before the normal process termination, the process is killed. The standard output and
181+
// standard error of the process are captured and returned at process termination.
182+
func (p *Process) RunAndCaptureOutput(ctx context.Context) ([]byte, []byte, error) {
183+
stdout := &bytes.Buffer{}
184+
stderr := &bytes.Buffer{}
185+
p.RedirectStdoutTo(stdout)
186+
p.RedirectStderrTo(stderr)
187+
err := p.RunWithinContext(ctx)
188+
return stdout.Bytes(), stderr.Bytes(), err
189+
}

‎legacy/builder/container_add_prototypes.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525

2626
bldr "github.com/arduino/arduino-cli/arduino/builder"
27+
"github.com/arduino/arduino-cli/arduino/builder/preprocessor"
2728
"github.com/arduino/arduino-cli/arduino/sketch"
2829
"github.com/arduino/arduino-cli/legacy/builder/types"
2930
"github.com/arduino/go-paths-helper"
@@ -62,10 +63,18 @@ func PreprocessSketchWithCtags(ctx *types.Context) error {
6263
ctx.SketchSourceAfterCppPreprocessing = filterSketchSource(ctx.Sketch, bytes.NewReader(src), false)
6364
}
6465

65-
if err := (&CTagsRunner{Source: &ctx.SketchSourceAfterCppPreprocessing, TargetFileName: "sketch_merged.cpp"}).Run(ctx); err != nil {
66-
return errors.WithStack(err)
66+
if err := targetFilePath.WriteFile([]byte(ctx.SketchSourceAfterCppPreprocessing)); err != nil {
67+
return err
68+
}
69+
70+
ctagsStdout, ctagsStderr, err := preprocessor.RunCTags(targetFilePath, ctx.BuildProperties)
71+
if ctx.Verbose {
72+
ctx.WriteStderr(ctagsStderr)
73+
}
74+
if err != nil {
75+
return err
6776
}
68-
ctx.SketchSourceAfterArduinoPreprocessing, ctx.PrototypesSection = PrototypesAdder(ctx.SketchSourceMerged, ctx.PrototypesLineWhereToInsert, ctx.LineOffset, ctx.Prototypes, ctx.DebugPreprocessor)
77+
ctx.SketchSourceAfterArduinoPreprocessing = PrototypesAdder(ctx.Sketch, ctx.SketchSourceMerged, ctagsStdout, ctx.LineOffset)
6978

7079
if err := bldr.SketchSaveItemCpp(ctx.Sketch.MainFile, []byte(ctx.SketchSourceAfterArduinoPreprocessing), ctx.SketchBuildPath); err != nil {
7180
return errors.WithStack(err)

‎legacy/builder/ctags_runner.go

-89
This file was deleted.

‎legacy/builder/prototypes_adder.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,37 @@ import (
2020
"strconv"
2121
"strings"
2222

23+
"github.com/arduino/arduino-cli/arduino/sketch"
2324
"github.com/arduino/arduino-cli/legacy/builder/constants"
2425
"github.com/arduino/arduino-cli/legacy/builder/ctags"
2526
"github.com/arduino/arduino-cli/legacy/builder/utils"
2627
)
2728

28-
func PrototypesAdder(source string, firstFunctionLine, lineOffset int, prototypes []*ctags.Prototype, debugOutput bool) (preprocessedSource, prototypeSection string) {
29+
var DebugPreprocessor bool
30+
31+
func PrototypesAdder(sketch *sketch.Sketch, source string, ctagsStdout []byte, lineOffset int) string {
32+
parser := &ctags.CTagsParser{}
33+
parser.Parse(ctagsStdout, sketch.MainFile)
34+
parser.FixCLinkageTagsDeclarations()
35+
36+
prototypes, firstFunctionLine := parser.GeneratePrototypes()
37+
if firstFunctionLine == -1 {
38+
firstFunctionLine = 0
39+
}
40+
2941
source = strings.Replace(source, "\r\n", "\n", -1)
3042
source = strings.Replace(source, "\r", "\n", -1)
3143
sourceRows := strings.Split(source, "\n")
3244
if isFirstFunctionOutsideOfSource(firstFunctionLine, sourceRows) {
33-
return
45+
return ""
3446
}
3547

3648
insertionLine := firstFunctionLine + lineOffset - 1
3749
firstFunctionChar := len(strings.Join(sourceRows[:insertionLine], "\n")) + 1
38-
prototypeSection = composePrototypeSection(firstFunctionLine, prototypes)
39-
preprocessedSource = source[:firstFunctionChar] + prototypeSection + source[firstFunctionChar:]
50+
prototypeSection := composePrototypeSection(firstFunctionLine, prototypes)
51+
preprocessedSource := source[:firstFunctionChar] + prototypeSection + source[firstFunctionChar:]
4052

41-
if debugOutput {
53+
if DebugPreprocessor {
4254
fmt.Println("#PREPROCESSED SOURCE")
4355
prototypesRows := strings.Split(prototypeSection, "\n")
4456
prototypesRows = prototypesRows[:len(prototypesRows)-1]
@@ -53,7 +65,7 @@ func PrototypesAdder(source string, firstFunctionLine, lineOffset int, prototype
5365
}
5466
fmt.Println("#END OF PREPROCESSED SOURCE")
5567
}
56-
return
68+
return preprocessedSource
5769
}
5870

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

‎legacy/builder/test/ctags_runner_test.go

+29-109
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,35 @@ import (
2020
"testing"
2121

2222
bldr "github.com/arduino/arduino-cli/arduino/builder"
23+
"github.com/arduino/arduino-cli/arduino/builder/preprocessor"
2324
"github.com/arduino/arduino-cli/legacy/builder"
24-
"github.com/arduino/arduino-cli/legacy/builder/types"
2525
paths "github.com/arduino/go-paths-helper"
2626
"github.com/stretchr/testify/require"
2727
)
2828

29-
func TestCTagsRunner(t *testing.T) {
30-
sketchLocation := Abs(t, paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"))
29+
func ctagsRunnerTestTemplate(t *testing.T, sketchLocation *paths.Path) []byte {
3130
ctx := prepareBuilderTestContext(t, nil, sketchLocation, "arduino:avr:leonardo")
3231
defer cleanUpBuilderTestContext(t, ctx)
3332
ctx.Verbose = true
3433

35-
ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"}
36-
var _err error
37-
commands := []types.Command{
38-
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
39-
types.BareCommand(func(ctx *types.Context) error {
40-
ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath)
41-
return _err
42-
}),
43-
&builder.ContainerFindIncludes{},
44-
&builder.PrintUsedLibrariesIfVerbose{},
45-
&builder.WarnAboutArchIncompatibleLibraries{},
46-
ctagsRunner,
47-
}
48-
for _, command := range commands {
49-
err := command.Run(ctx)
50-
NoError(t, err)
51-
}
34+
err := (&builder.ContainerSetupHardwareToolsLibsSketchAndProps{}).Run(ctx)
35+
NoError(t, err)
36+
37+
_, source, err := bldr.PrepareSketchBuildPath(ctx.Sketch, nil, ctx.SketchBuildPath)
38+
NoError(t, err)
39+
40+
target := ctx.BuildPath.Join("ctags_target.cpp")
41+
NoError(t, target.WriteFile([]byte(source)))
42+
43+
ctagsOutput, _, err := preprocessor.RunCTags(target, ctx.BuildProperties)
44+
NoError(t, err)
45+
46+
return ctagsOutput
47+
}
48+
49+
func TestCTagsRunner(t *testing.T) {
50+
sketchLocation := Abs(t, paths.New("downloaded_libraries", "Bridge", "examples", "Bridge", "Bridge.ino"))
51+
ctagsOutput := ctagsRunnerTestTemplate(t, sketchLocation)
5252

5353
quotedSketchLocation := strings.Replace(sketchLocation.String(), "\\", "\\\\", -1)
5454
expectedOutput := "server " + quotedSketchLocation + " /^BridgeServer server;$/;\" kind:variable line:31\n" +
@@ -58,133 +58,53 @@ func TestCTagsRunner(t *testing.T) {
5858
"digitalCommand " + quotedSketchLocation + " /^void digitalCommand(BridgeClient client) {$/;\" kind:function line:82 signature:(BridgeClient client) returntype:void\n" +
5959
"analogCommand " + quotedSketchLocation + " /^void analogCommand(BridgeClient client) {$/;\" kind:function line:109 signature:(BridgeClient client) returntype:void\n" +
6060
"modeCommand " + quotedSketchLocation + " /^void modeCommand(BridgeClient client) {$/;\" kind:function line:149 signature:(BridgeClient client) returntype:void\n"
61-
require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1))
61+
require.Equal(t, expectedOutput, strings.Replace(string(ctagsOutput), "\r\n", "\n", -1))
6262
}
6363

6464
func TestCTagsRunnerSketchWithClass(t *testing.T) {
6565
sketchLocation := Abs(t, paths.New("sketch_with_class", "sketch_with_class.ino"))
66-
ctx := prepareBuilderTestContext(t, nil, sketchLocation, "arduino:avr:leonardo")
67-
defer cleanUpBuilderTestContext(t, ctx)
68-
ctx.Verbose = true
69-
70-
ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"}
71-
var _err error
72-
commands := []types.Command{
73-
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
74-
types.BareCommand(func(ctx *types.Context) error {
75-
ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath)
76-
return _err
77-
}),
78-
&builder.ContainerFindIncludes{},
79-
&builder.PrintUsedLibrariesIfVerbose{},
80-
&builder.WarnAboutArchIncompatibleLibraries{},
81-
ctagsRunner,
82-
}
83-
for _, command := range commands {
84-
err := command.Run(ctx)
85-
NoError(t, err)
86-
}
66+
ctagsOutput := ctagsRunnerTestTemplate(t, sketchLocation)
8767

8868
quotedSketchLocation := strings.Replace(sketchLocation.String(), "\\", "\\\\", -1)
8969
expectedOutput := "set_values\t" + quotedSketchLocation + "\t/^ void set_values (int,int);$/;\"\tkind:prototype\tline:4\tclass:Rectangle\tsignature:(int,int)\treturntype:void\n" +
9070
"area\t" + quotedSketchLocation + "\t/^ int area() {return width*height;}$/;\"\tkind:function\tline:5\tclass:Rectangle\tsignature:()\treturntype:int\n" +
9171
"set_values\t" + quotedSketchLocation + "\t/^void Rectangle::set_values (int x, int y) {$/;\"\tkind:function\tline:8\tclass:Rectangle\tsignature:(int x, int y)\treturntype:void\n" +
9272
"setup\t" + quotedSketchLocation + "\t/^void setup() {$/;\"\tkind:function\tline:13\tsignature:()\treturntype:void\n" +
9373
"loop\t" + quotedSketchLocation + "\t/^void loop() {$/;\"\tkind:function\tline:17\tsignature:()\treturntype:void\n"
94-
require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1))
74+
require.Equal(t, expectedOutput, strings.Replace(string(ctagsOutput), "\r\n", "\n", -1))
9575
}
9676

9777
func TestCTagsRunnerSketchWithTypename(t *testing.T) {
9878
sketchLocation := Abs(t, paths.New("sketch_with_typename", "sketch_with_typename.ino"))
99-
ctx := prepareBuilderTestContext(t, nil, sketchLocation, "arduino:avr:leonardo")
100-
defer cleanUpBuilderTestContext(t, ctx)
101-
ctx.Verbose = true
102-
103-
ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"}
104-
var _err error
105-
commands := []types.Command{
106-
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
107-
types.BareCommand(func(ctx *types.Context) error {
108-
ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath)
109-
return _err
110-
}),
111-
&builder.ContainerFindIncludes{},
112-
&builder.PrintUsedLibrariesIfVerbose{},
113-
&builder.WarnAboutArchIncompatibleLibraries{},
114-
ctagsRunner,
115-
}
116-
for _, command := range commands {
117-
err := command.Run(ctx)
118-
NoError(t, err)
119-
}
79+
ctagsOutput := ctagsRunnerTestTemplate(t, sketchLocation)
12080

12181
quotedSketchLocation := strings.Replace(sketchLocation.String(), "\\", "\\\\", -1)
12282
expectedOutput := "Foo\t" + quotedSketchLocation + "\t/^ struct Foo{$/;\"\tkind:struct\tline:2\n" +
12383
"setup\t" + quotedSketchLocation + "\t/^void setup() {$/;\"\tkind:function\tline:6\tsignature:()\treturntype:void\n" +
12484
"loop\t" + quotedSketchLocation + "\t/^void loop() {}$/;\"\tkind:function\tline:10\tsignature:()\treturntype:void\n" +
12585
"func\t" + quotedSketchLocation + "\t/^typename Foo<char>::Bar func(){$/;\"\tkind:function\tline:12\tsignature:()\treturntype:Foo::Bar\n"
126-
require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1))
86+
require.Equal(t, expectedOutput, strings.Replace(string(ctagsOutput), "\r\n", "\n", -1))
12787
}
12888

12989
func TestCTagsRunnerSketchWithNamespace(t *testing.T) {
13090
sketchLocation := Abs(t, paths.New("sketch_with_namespace", "sketch_with_namespace.ino"))
131-
ctx := prepareBuilderTestContext(t, nil, sketchLocation, "arduino:avr:leonardo")
132-
defer cleanUpBuilderTestContext(t, ctx)
133-
ctx.Verbose = true
134-
135-
ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"}
136-
var _err error
137-
commands := []types.Command{
138-
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
139-
types.BareCommand(func(ctx *types.Context) error {
140-
ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath)
141-
return _err
142-
}),
143-
&builder.ContainerFindIncludes{},
144-
&builder.PrintUsedLibrariesIfVerbose{},
145-
&builder.WarnAboutArchIncompatibleLibraries{},
146-
ctagsRunner,
147-
}
148-
for _, command := range commands {
149-
err := command.Run(ctx)
150-
NoError(t, err)
151-
}
91+
ctagsOutput := ctagsRunnerTestTemplate(t, sketchLocation)
15292

15393
quotedSketchLocation := strings.Replace(sketchLocation.String(), "\\", "\\\\", -1)
15494
expectedOutput := "value\t" + quotedSketchLocation + "\t/^\tint value() {$/;\"\tkind:function\tline:2\tnamespace:Test\tsignature:()\treturntype:int\n" +
15595
"setup\t" + quotedSketchLocation + "\t/^void setup() {}$/;\"\tkind:function\tline:7\tsignature:()\treturntype:void\n" +
15696
"loop\t" + quotedSketchLocation + "\t/^void loop() {}$/;\"\tkind:function\tline:8\tsignature:()\treturntype:void\n"
157-
require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1))
97+
require.Equal(t, expectedOutput, strings.Replace(string(ctagsOutput), "\r\n", "\n", -1))
15898
}
15999

160100
func TestCTagsRunnerSketchWithTemplates(t *testing.T) {
161101
sketchLocation := Abs(t, paths.New("sketch_with_templates_and_shift", "sketch_with_templates_and_shift.ino"))
162-
ctx := prepareBuilderTestContext(t, nil, sketchLocation, "arduino:avr:leonardo")
163-
defer cleanUpBuilderTestContext(t, ctx)
164-
ctx.Verbose = true
165-
166-
ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"}
167-
var _err error
168-
commands := []types.Command{
169-
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
170-
types.BareCommand(func(ctx *types.Context) error {
171-
ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath)
172-
return _err
173-
}),
174-
&builder.ContainerFindIncludes{},
175-
&builder.PrintUsedLibrariesIfVerbose{},
176-
&builder.WarnAboutArchIncompatibleLibraries{},
177-
ctagsRunner,
178-
}
179-
for _, command := range commands {
180-
err := command.Run(ctx)
181-
NoError(t, err)
182-
}
102+
ctagsOutput := ctagsRunnerTestTemplate(t, sketchLocation)
183103

184104
quotedSketchLocation := strings.Replace(sketchLocation.String(), "\\", "\\\\", -1)
185105
expectedOutput := "printGyro\t" + quotedSketchLocation + "\t/^void printGyro()$/;\"\tkind:function\tline:10\tsignature:()\treturntype:void\n" +
186106
"bVar\t" + quotedSketchLocation + "\t/^c< 8 > bVar;$/;\"\tkind:variable\tline:15\n" +
187107
"aVar\t" + quotedSketchLocation + "\t/^c< 1<<8 > aVar;$/;\"\tkind:variable\tline:16\n" +
188108
"func\t" + quotedSketchLocation + "\t/^template<int X> func( c< 1<<X> & aParam) {$/;\"\tkind:function\tline:18\tsignature:( c< 1<<X> & aParam)\treturntype:template\n"
189-
require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1))
109+
require.Equal(t, expectedOutput, strings.Replace(string(ctagsOutput), "\r\n", "\n", -1))
190110
}

‎legacy/builder/test/prototypes_adder_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestPrototypesAdderBridgeExample(t *testing.T) {
5454
NoError(t, builder.PreprocessSketchWithCtags(ctx))
5555

5656
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include <Arduino.h>\n#line 1 "+quotedSketchLocation+"\n")
57-
require.Equal(t, "#line 33 "+quotedSketchLocation+"\nvoid setup();\n#line 46 "+quotedSketchLocation+"\nvoid loop();\n#line 62 "+quotedSketchLocation+"\nvoid process(BridgeClient client);\n#line 82 "+quotedSketchLocation+"\nvoid digitalCommand(BridgeClient client);\n#line 109 "+quotedSketchLocation+"\nvoid analogCommand(BridgeClient client);\n#line 149 "+quotedSketchLocation+"\nvoid modeCommand(BridgeClient client);\n#line 33 "+quotedSketchLocation+"\n", ctx.PrototypesSection)
57+
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#line 33 "+quotedSketchLocation+"\nvoid setup();\n#line 46 "+quotedSketchLocation+"\nvoid loop();\n#line 62 "+quotedSketchLocation+"\nvoid process(BridgeClient client);\n#line 82 "+quotedSketchLocation+"\nvoid digitalCommand(BridgeClient client);\n#line 109 "+quotedSketchLocation+"\nvoid analogCommand(BridgeClient client);\n#line 149 "+quotedSketchLocation+"\nvoid modeCommand(BridgeClient client);\n#line 33 "+quotedSketchLocation+"\n")
5858
}
5959

6060
func TestPrototypesAdderSketchWithIfDef(t *testing.T) {
@@ -262,7 +262,7 @@ func TestPrototypesAdderSketchWithConfig(t *testing.T) {
262262
NoError(t, builder.PreprocessSketchWithCtags(ctx))
263263

264264
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include <Arduino.h>\n#line 1 "+quotedSketchLocation+"\n")
265-
require.Equal(t, "#line 13 "+quotedSketchLocation+"\nvoid setup();\n#line 17 "+quotedSketchLocation+"\nvoid loop();\n#line 13 "+quotedSketchLocation+"\n", ctx.PrototypesSection)
265+
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#line 13 "+quotedSketchLocation+"\nvoid setup();\n#line 17 "+quotedSketchLocation+"\nvoid loop();\n#line 13 "+quotedSketchLocation+"\n")
266266

267267
preprocessed := LoadAndInterpolate(t, filepath.Join("sketch_with_config", "sketch_with_config.preprocessed.txt"), ctx)
268268
require.Equal(t, preprocessed, strings.Replace(ctx.SketchSourceAfterArduinoPreprocessing, "\r\n", "\n", -1))
@@ -293,7 +293,7 @@ func TestPrototypesAdderSketchNoFunctionsTwoFiles(t *testing.T) {
293293
NoError(t, builder.PreprocessSketchWithCtags(ctx))
294294

295295
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include <Arduino.h>\n#line 1 "+quotedSketchLocation+"\n")
296-
require.Equal(t, "", ctx.PrototypesSection)
296+
require.Equal(t, ctx.SketchSourceMerged, ctx.SketchSourceAfterArduinoPreprocessing) // No prototypes added
297297
}
298298

299299
func TestPrototypesAdderSketchNoFunctions(t *testing.T) {
@@ -320,7 +320,7 @@ func TestPrototypesAdderSketchNoFunctions(t *testing.T) {
320320
NoError(t, builder.PreprocessSketchWithCtags(ctx))
321321

322322
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include <Arduino.h>\n#line 1 "+quotedSketchLocation+"\n")
323-
require.Equal(t, "", ctx.PrototypesSection)
323+
require.Equal(t, ctx.SketchSourceMerged, ctx.SketchSourceAfterArduinoPreprocessing) // No prototypes added
324324
}
325325

326326
func TestPrototypesAdderSketchWithDefaultArgs(t *testing.T) {
@@ -348,7 +348,7 @@ func TestPrototypesAdderSketchWithDefaultArgs(t *testing.T) {
348348
NoError(t, builder.PreprocessSketchWithCtags(ctx))
349349

350350
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include <Arduino.h>\n#line 1 "+quotedSketchLocation+"\n")
351-
require.Equal(t, "#line 4 "+quotedSketchLocation+"\nvoid setup();\n#line 7 "+quotedSketchLocation+"\nvoid loop();\n#line 1 "+quotedSketchLocation+"\n", ctx.PrototypesSection)
351+
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#line 4 "+quotedSketchLocation+"\nvoid setup();\n#line 7 "+quotedSketchLocation+"\nvoid loop();\n#line 1 "+quotedSketchLocation+"\n")
352352
}
353353

354354
func TestPrototypesAdderSketchWithInlineFunction(t *testing.T) {
@@ -378,7 +378,7 @@ func TestPrototypesAdderSketchWithInlineFunction(t *testing.T) {
378378
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include <Arduino.h>\n#line 1 "+quotedSketchLocation+"\n")
379379

380380
expected := "#line 1 " + quotedSketchLocation + "\nvoid setup();\n#line 2 " + quotedSketchLocation + "\nvoid loop();\n#line 4 " + quotedSketchLocation + "\nshort unsigned int testInt();\n#line 8 " + quotedSketchLocation + "\nstatic int8_t testInline();\n#line 12 " + quotedSketchLocation + "\n__attribute__((always_inline)) uint8_t testAttribute();\n#line 1 " + quotedSketchLocation + "\n"
381-
obtained := ctx.PrototypesSection
381+
obtained := ctx.SketchSourceAfterArduinoPreprocessing
382382
// ctags based preprocessing removes "inline" but this is still OK
383383
// TODO: remove this exception when moving to a more powerful parser
384384
expected = strings.Replace(expected, "static inline int8_t testInline();", "static int8_t testInline();", -1)
@@ -387,7 +387,7 @@ func TestPrototypesAdderSketchWithInlineFunction(t *testing.T) {
387387
// TODO: remove this exception when moving to a more powerful parser
388388
expected = strings.Replace(expected, "__attribute__((always_inline)) uint8_t testAttribute();", "uint8_t testAttribute();", -1)
389389
obtained = strings.Replace(obtained, "__attribute__((always_inline)) uint8_t testAttribute();", "uint8_t testAttribute();", -1)
390-
require.Equal(t, expected, obtained)
390+
require.Contains(t, obtained, expected)
391391
}
392392

393393
func TestPrototypesAdderSketchWithFunctionSignatureInsideIFDEF(t *testing.T) {
@@ -415,7 +415,7 @@ func TestPrototypesAdderSketchWithFunctionSignatureInsideIFDEF(t *testing.T) {
415415
NoError(t, builder.PreprocessSketchWithCtags(ctx))
416416

417417
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include <Arduino.h>\n#line 1 "+quotedSketchLocation+"\n")
418-
require.Equal(t, "#line 1 "+quotedSketchLocation+"\nvoid setup();\n#line 3 "+quotedSketchLocation+"\nvoid loop();\n#line 15 "+quotedSketchLocation+"\nint8_t adalight();\n#line 1 "+quotedSketchLocation+"\n", ctx.PrototypesSection)
418+
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#line 1 "+quotedSketchLocation+"\nvoid setup();\n#line 3 "+quotedSketchLocation+"\nvoid loop();\n#line 15 "+quotedSketchLocation+"\nint8_t adalight();\n#line 1 "+quotedSketchLocation+"\n")
419419
}
420420

421421
func TestPrototypesAdderSketchWithUSBCON(t *testing.T) {
@@ -448,7 +448,7 @@ func TestPrototypesAdderSketchWithUSBCON(t *testing.T) {
448448
NoError(t, builder.PreprocessSketchWithCtags(ctx))
449449

450450
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include <Arduino.h>\n#line 1 "+quotedSketchLocation+"\n")
451-
require.Equal(t, "#line 5 "+quotedSketchLocation+"\nvoid ciao();\n#line 10 "+quotedSketchLocation+"\nvoid setup();\n#line 15 "+quotedSketchLocation+"\nvoid loop();\n#line 5 "+quotedSketchLocation+"\n", ctx.PrototypesSection)
451+
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#line 5 "+quotedSketchLocation+"\nvoid ciao();\n#line 10 "+quotedSketchLocation+"\nvoid setup();\n#line 15 "+quotedSketchLocation+"\nvoid loop();\n#line 5 "+quotedSketchLocation+"\n")
452452
}
453453

454454
func TestPrototypesAdderSketchWithTypename(t *testing.T) {
@@ -481,12 +481,12 @@ func TestPrototypesAdderSketchWithTypename(t *testing.T) {
481481

482482
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include <Arduino.h>\n#line 1 "+quotedSketchLocation+"\n")
483483
expected := "#line 6 " + quotedSketchLocation + "\nvoid setup();\n#line 10 " + quotedSketchLocation + "\nvoid loop();\n#line 12 " + quotedSketchLocation + "\ntypename Foo<char>::Bar func();\n#line 6 " + quotedSketchLocation + "\n"
484-
obtained := ctx.PrototypesSection
484+
obtained := ctx.SketchSourceAfterArduinoPreprocessing
485485
// ctags based preprocessing ignores line with typename
486486
// TODO: remove this exception when moving to a more powerful parser
487487
expected = strings.Replace(expected, "#line 12 "+quotedSketchLocation+"\ntypename Foo<char>::Bar func();\n", "", -1)
488488
obtained = strings.Replace(obtained, "#line 12 "+quotedSketchLocation+"\ntypename Foo<char>::Bar func();\n", "", -1)
489-
require.Equal(t, expected, obtained)
489+
require.Contains(t, obtained, expected)
490490
}
491491

492492
func TestPrototypesAdderSketchWithIfDef2(t *testing.T) {
@@ -514,7 +514,7 @@ func TestPrototypesAdderSketchWithIfDef2(t *testing.T) {
514514
NoError(t, builder.PreprocessSketchWithCtags(ctx))
515515

516516
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include <Arduino.h>\n#line 1 "+quotedSketchLocation+"\n")
517-
require.Equal(t, "#line 5 "+quotedSketchLocation+"\nvoid elseBranch();\n#line 9 "+quotedSketchLocation+"\nvoid f1();\n#line 10 "+quotedSketchLocation+"\nvoid f2();\n#line 12 "+quotedSketchLocation+"\nvoid setup();\n#line 14 "+quotedSketchLocation+"\nvoid loop();\n#line 5 "+quotedSketchLocation+"\n", ctx.PrototypesSection)
517+
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#line 5 "+quotedSketchLocation+"\nvoid elseBranch();\n#line 9 "+quotedSketchLocation+"\nvoid f1();\n#line 10 "+quotedSketchLocation+"\nvoid f2();\n#line 12 "+quotedSketchLocation+"\nvoid setup();\n#line 14 "+quotedSketchLocation+"\nvoid loop();\n#line 5 "+quotedSketchLocation+"\n")
518518

519519
expectedSource := LoadAndInterpolate(t, filepath.Join("sketch_with_ifdef", "sketch.preprocessed.txt"), ctx)
520520
require.Equal(t, expectedSource, strings.Replace(ctx.SketchSourceAfterArduinoPreprocessing, "\r\n", "\n", -1))
@@ -545,7 +545,7 @@ func TestPrototypesAdderSketchWithIfDef2SAM(t *testing.T) {
545545
NoError(t, builder.PreprocessSketchWithCtags(ctx))
546546

547547
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include <Arduino.h>\n#line 1 "+quotedSketchLocation+"\n")
548-
require.Equal(t, "#line 2 "+quotedSketchLocation+"\nvoid ifBranch();\n#line 9 "+quotedSketchLocation+"\nvoid f1();\n#line 10 "+quotedSketchLocation+"\nvoid f2();\n#line 12 "+quotedSketchLocation+"\nvoid setup();\n#line 14 "+quotedSketchLocation+"\nvoid loop();\n#line 2 "+quotedSketchLocation+"\n", ctx.PrototypesSection)
548+
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#line 2 "+quotedSketchLocation+"\nvoid ifBranch();\n#line 9 "+quotedSketchLocation+"\nvoid f1();\n#line 10 "+quotedSketchLocation+"\nvoid f2();\n#line 12 "+quotedSketchLocation+"\nvoid setup();\n#line 14 "+quotedSketchLocation+"\nvoid loop();\n#line 2 "+quotedSketchLocation+"\n")
549549

550550
expectedSource := LoadAndInterpolate(t, filepath.Join("sketch_with_ifdef", "sketch.preprocessed.SAM.txt"), ctx)
551551
require.Equal(t, expectedSource, strings.Replace(ctx.SketchSourceAfterArduinoPreprocessing, "\r\n", "\n", -1))
@@ -576,7 +576,7 @@ func TestPrototypesAdderSketchWithConst(t *testing.T) {
576576
NoError(t, builder.PreprocessSketchWithCtags(ctx))
577577

578578
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include <Arduino.h>\n#line 1 "+quotedSketchLocation+"\n")
579-
require.Equal(t, "#line 1 "+quotedSketchLocation+"\nvoid setup();\n#line 2 "+quotedSketchLocation+"\nvoid loop();\n#line 4 "+quotedSketchLocation+"\nconst __FlashStringHelper* test();\n#line 6 "+quotedSketchLocation+"\nconst int test3();\n#line 8 "+quotedSketchLocation+"\nvolatile __FlashStringHelper* test2();\n#line 10 "+quotedSketchLocation+"\nvolatile int test4();\n#line 1 "+quotedSketchLocation+"\n", ctx.PrototypesSection)
579+
require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#line 1 "+quotedSketchLocation+"\nvoid setup();\n#line 2 "+quotedSketchLocation+"\nvoid loop();\n#line 4 "+quotedSketchLocation+"\nconst __FlashStringHelper* test();\n#line 6 "+quotedSketchLocation+"\nconst int test3();\n#line 8 "+quotedSketchLocation+"\nvolatile __FlashStringHelper* test2();\n#line 10 "+quotedSketchLocation+"\nvolatile int test4();\n#line 1 "+quotedSketchLocation+"\n")
580580
}
581581

582582
func TestPrototypesAdderSketchWithDosEol(t *testing.T) {

‎legacy/builder/test/try_build_of_problematic_sketch_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,13 @@ func TestTryBuild042(t *testing.T) {
202202
}
203203

204204
func makeDefaultContext() *types.Context {
205+
builder.DebugPreprocessor = true
205206
return &types.Context{
206207
HardwareDirs: paths.NewPathList(filepath.Join("..", "hardware"), "downloaded_hardware", "downloaded_board_manager_stuff"),
207208
BuiltInToolsDirs: paths.NewPathList("downloaded_tools"),
208209
BuiltInLibrariesDirs: paths.New("downloaded_libraries"),
209210
OtherLibrariesDirs: paths.NewPathList("libraries"),
210211
Verbose: true,
211-
DebugPreprocessor: true,
212212
}
213213
}
214214

‎legacy/builder/types/context.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
3030
"github.com/arduino/arduino-cli/arduino/libraries/librariesresolver"
3131
"github.com/arduino/arduino-cli/arduino/sketch"
32-
"github.com/arduino/arduino-cli/legacy/builder/ctags"
3332
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3433
paths "github.com/arduino/go-paths-helper"
3534
properties "github.com/arduino/go-properties-orderedmap"
@@ -119,14 +118,10 @@ type Context struct {
119118
UseCachedLibrariesResolution bool
120119

121120
// C++ Parsing
122-
LineOffset int
123-
PrototypesSection string
124-
PrototypesLineWhereToInsert int
125-
Prototypes []*ctags.Prototype
121+
LineOffset int
126122

127123
// Verbosity settings
128-
Verbose bool
129-
DebugPreprocessor bool
124+
Verbose bool
130125

131126
// Dry run, only create progress map
132127
Progress ProgressStruct

0 commit comments

Comments
 (0)
Please sign in to comment.