From 63f37296e07f39b92eaf90c2fb59cad1d036e635 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 3 Apr 2023 11:03:23 +0200 Subject: [PATCH 01/11] legacy: Removed ReadFileAndStoreInContext command --- legacy/builder/container_add_prototypes.go | 7 ++- .../builder/read_file_and_store_in_context.go | 38 ---------------- .../read_file_and_store_in_context_test.go | 44 ------------------- 3 files changed, 6 insertions(+), 83 deletions(-) delete mode 100644 legacy/builder/read_file_and_store_in_context.go delete mode 100644 legacy/builder/test/read_file_and_store_in_context_test.go diff --git a/legacy/builder/container_add_prototypes.go b/legacy/builder/container_add_prototypes.go index d710b82c979..dd0ab219936 100644 --- a/legacy/builder/container_add_prototypes.go +++ b/legacy/builder/container_add_prototypes.go @@ -47,8 +47,13 @@ func PreprocessSketchWithCtags(ctx *types.Context) error { } } + if src, err := targetFilePath.ReadFile(); err != nil { + return err + } else { + ctx.SourceGccMinusE = string(src) + } + commands := []types.Command{ - &ReadFileAndStoreInContext{FileToRead: targetFilePath, Target: &ctx.SourceGccMinusE}, &FilterSketchSource{Source: &ctx.SourceGccMinusE}, &CTagsTargetFileSaver{Source: &ctx.SourceGccMinusE, TargetFileName: "ctags_target_for_gcc_minus_e.cpp"}, &CTagsRunner{}, diff --git a/legacy/builder/read_file_and_store_in_context.go b/legacy/builder/read_file_and_store_in_context.go deleted file mode 100644 index cdd75896387..00000000000 --- a/legacy/builder/read_file_and_store_in_context.go +++ /dev/null @@ -1,38 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package builder - -import ( - "github.com/arduino/arduino-cli/legacy/builder/types" - "github.com/arduino/go-paths-helper" - "github.com/pkg/errors" -) - -type ReadFileAndStoreInContext struct { - FileToRead *paths.Path - Target *string -} - -func (s *ReadFileAndStoreInContext) Run(ctx *types.Context) error { - bytes, err := s.FileToRead.ReadFile() - if err != nil { - return errors.WithStack(err) - } - - *s.Target = string(bytes) - - return nil -} diff --git a/legacy/builder/test/read_file_and_store_in_context_test.go b/legacy/builder/test/read_file_and_store_in_context_test.go deleted file mode 100644 index c4e299e2c60..00000000000 --- a/legacy/builder/test/read_file_and_store_in_context_test.go +++ /dev/null @@ -1,44 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package test - -import ( - "os" - "testing" - - "github.com/arduino/arduino-cli/legacy/builder" - "github.com/arduino/arduino-cli/legacy/builder/types" - paths "github.com/arduino/go-paths-helper" - "github.com/stretchr/testify/require" -) - -func TestReadFileAndStoreInContext(t *testing.T) { - filePath, err := os.CreateTemp("", "test") - NoError(t, err) - - file := paths.New(filePath.Name()) - defer file.RemoveAll() - - file.WriteFile([]byte("test test\nciao")) - - ctx := &types.Context{} - - command := &builder.ReadFileAndStoreInContext{FileToRead: file, Target: &ctx.SourceGccMinusE} - err = command.Run(ctx) - NoError(t, err) - - require.Equal(t, "test test\nciao", ctx.SourceGccMinusE) -} From c0979bc8db81eee05dc0075480ea486d557e8a11 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 30 Mar 2023 17:00:27 +0200 Subject: [PATCH 02/11] ctags parser.Parse signature change --- legacy/builder/ctags/ctags_parser.go | 4 ++-- legacy/builder/ctags/ctags_parser_test.go | 2 +- legacy/builder/ctags/ctags_to_prototypes_test.go | 2 +- legacy/builder/ctags_runner.go | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/legacy/builder/ctags/ctags_parser.go b/legacy/builder/ctags/ctags_parser.go index 4ced464ae1e..a2c49851338 100644 --- a/legacy/builder/ctags/ctags_parser.go +++ b/legacy/builder/ctags/ctags_parser.go @@ -43,8 +43,8 @@ type CTagsParser struct { mainFile *paths.Path } -func (p *CTagsParser) Parse(ctagsOutput string, mainFile *paths.Path) []*types.CTag { - rows := strings.Split(ctagsOutput, "\n") +func (p *CTagsParser) Parse(ctagsOutput []byte, mainFile *paths.Path) []*types.CTag { + rows := strings.Split(string(ctagsOutput), "\n") rows = removeEmpty(rows) p.mainFile = mainFile diff --git a/legacy/builder/ctags/ctags_parser_test.go b/legacy/builder/ctags/ctags_parser_test.go index d3bb03b23a0..49711c27c18 100644 --- a/legacy/builder/ctags/ctags_parser_test.go +++ b/legacy/builder/ctags/ctags_parser_test.go @@ -30,7 +30,7 @@ func produceTags(t *testing.T, filename string) []*types.CTag { require.NoError(t, err) parser := CTagsParser{} - return parser.Parse(string(bytes), nil) + return parser.Parse(bytes, nil) } func TestCTagsParserShouldListPrototypes(t *testing.T) { diff --git a/legacy/builder/ctags/ctags_to_prototypes_test.go b/legacy/builder/ctags/ctags_to_prototypes_test.go index e73a1312069..7dcea2a0ab1 100644 --- a/legacy/builder/ctags/ctags_to_prototypes_test.go +++ b/legacy/builder/ctags/ctags_to_prototypes_test.go @@ -30,7 +30,7 @@ func producePrototypes(t *testing.T, filename string, mainFile string) ([]*types require.NoError(t, err) parser := &CTagsParser{} - parser.Parse(string(bytes), paths.New(mainFile)) + parser.Parse(bytes, paths.New(mainFile)) return parser.GeneratePrototypes() } diff --git a/legacy/builder/ctags_runner.go b/legacy/builder/ctags_runner.go index 2e42bdca234..7dd91f578a0 100644 --- a/legacy/builder/ctags_runner.go +++ b/legacy/builder/ctags_runner.go @@ -61,7 +61,7 @@ func (s *CTagsRunner) Run(ctx *types.Context) error { parser := &ctags.CTagsParser{} - ctx.CTagsOfPreprocessedSource = parser.Parse(ctx.CTagsOutput, ctx.Sketch.MainFile) + ctx.CTagsOfPreprocessedSource = parser.Parse(sourceBytes, ctx.Sketch.MainFile) parser.FixCLinkageTagsDeclarations(ctx.CTagsOfPreprocessedSource) protos, line := parser.GeneratePrototypes() From 6ed3298e0967dec40ad0419371dc24ec13366988 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 30 Mar 2023 17:04:53 +0200 Subject: [PATCH 03/11] removing ctags-related data from builder ctx (part 1) --- legacy/builder/ctags_runner.go | 16 ++++++++------- legacy/builder/test/ctags_runner_test.go | 26 ++++++++++++++---------- legacy/builder/types/context.go | 1 - 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/legacy/builder/ctags_runner.go b/legacy/builder/ctags_runner.go index 7dd91f578a0..edf46a3d69a 100644 --- a/legacy/builder/ctags_runner.go +++ b/legacy/builder/ctags_runner.go @@ -26,9 +26,12 @@ import ( "github.com/pkg/errors" ) -type CTagsRunner struct{} +type CTagsRunner struct { + // Needed for unit-testing + CtagsOutput []byte +} -func (s *CTagsRunner) Run(ctx *types.Context) error { +func (r *CTagsRunner) Run(ctx *types.Context) error { ctagsTargetFilePath := ctx.CTagsTargetFile buildProperties := properties.NewMap() @@ -52,16 +55,13 @@ func (s *CTagsRunner) Run(ctx *types.Context) error { command := exec.Command(parts[0], parts[1:]...) command.Env = append(os.Environ(), ctx.PackageManager.GetEnvVarsForSpawnedProcess()...) - sourceBytes, _, err := utils.ExecCommand(ctx, command, utils.Capture /* stdout */, utils.ShowIfVerbose /* stderr */) + ctagsOutput, _, err := utils.ExecCommand(ctx, command, utils.Capture /* stdout */, utils.ShowIfVerbose /* stderr */) if err != nil { return errors.WithStack(err) } - ctx.CTagsOutput = string(sourceBytes) - parser := &ctags.CTagsParser{} - - ctx.CTagsOfPreprocessedSource = parser.Parse(sourceBytes, ctx.Sketch.MainFile) + ctx.CTagsOfPreprocessedSource = parser.Parse(ctagsOutput, ctx.Sketch.MainFile) parser.FixCLinkageTagsDeclarations(ctx.CTagsOfPreprocessedSource) protos, line := parser.GeneratePrototypes() @@ -70,5 +70,7 @@ func (s *CTagsRunner) Run(ctx *types.Context) error { } ctx.Prototypes = protos + // Needed for unit-testing + r.CtagsOutput = ctagsOutput return nil } diff --git a/legacy/builder/test/ctags_runner_test.go b/legacy/builder/test/ctags_runner_test.go index 013499a461c..b5b9e823dc5 100644 --- a/legacy/builder/test/ctags_runner_test.go +++ b/legacy/builder/test/ctags_runner_test.go @@ -31,6 +31,7 @@ func TestCTagsRunner(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true + ctagsRunner := &builder.CTagsRunner{} commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, @@ -38,7 +39,7 @@ func TestCTagsRunner(t *testing.T) { &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"}, - &builder.CTagsRunner{}, + ctagsRunner, } for _, command := range commands { err := command.Run(ctx) @@ -53,8 +54,7 @@ func TestCTagsRunner(t *testing.T) { "digitalCommand " + quotedSketchLocation + " /^void digitalCommand(BridgeClient client) {$/;\" kind:function line:82 signature:(BridgeClient client) returntype:void\n" + "analogCommand " + quotedSketchLocation + " /^void analogCommand(BridgeClient client) {$/;\" kind:function line:109 signature:(BridgeClient client) returntype:void\n" + "modeCommand " + quotedSketchLocation + " /^void modeCommand(BridgeClient client) {$/;\" kind:function line:149 signature:(BridgeClient client) returntype:void\n" - - require.Equal(t, expectedOutput, strings.Replace(ctx.CTagsOutput, "\r\n", "\n", -1)) + require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1)) } func TestCTagsRunnerSketchWithClass(t *testing.T) { @@ -63,6 +63,7 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true + ctagsRunner := &builder.CTagsRunner{} commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, @@ -70,7 +71,7 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) { &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"}, - &builder.CTagsRunner{}, + ctagsRunner, } for _, command := range commands { err := command.Run(ctx) @@ -83,7 +84,7 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) { "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" + "setup\t" + quotedSketchLocation + "\t/^void setup() {$/;\"\tkind:function\tline:13\tsignature:()\treturntype:void\n" + "loop\t" + quotedSketchLocation + "\t/^void loop() {$/;\"\tkind:function\tline:17\tsignature:()\treturntype:void\n" - require.Equal(t, expectedOutput, strings.Replace(ctx.CTagsOutput, "\r\n", "\n", -1)) + require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1)) } func TestCTagsRunnerSketchWithTypename(t *testing.T) { @@ -92,6 +93,7 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true + ctagsRunner := &builder.CTagsRunner{} commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, @@ -99,7 +101,7 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) { &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"}, - &builder.CTagsRunner{}, + ctagsRunner, } for _, command := range commands { err := command.Run(ctx) @@ -111,7 +113,7 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) { "setup\t" + quotedSketchLocation + "\t/^void setup() {$/;\"\tkind:function\tline:6\tsignature:()\treturntype:void\n" + "loop\t" + quotedSketchLocation + "\t/^void loop() {}$/;\"\tkind:function\tline:10\tsignature:()\treturntype:void\n" + "func\t" + quotedSketchLocation + "\t/^typename Foo::Bar func(){$/;\"\tkind:function\tline:12\tsignature:()\treturntype:Foo::Bar\n" - require.Equal(t, expectedOutput, strings.Replace(ctx.CTagsOutput, "\r\n", "\n", -1)) + require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1)) } func TestCTagsRunnerSketchWithNamespace(t *testing.T) { @@ -120,6 +122,7 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true + ctagsRunner := &builder.CTagsRunner{} commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, @@ -127,7 +130,7 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) { &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"}, - &builder.CTagsRunner{}, + ctagsRunner, } for _, command := range commands { err := command.Run(ctx) @@ -138,7 +141,7 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) { expectedOutput := "value\t" + quotedSketchLocation + "\t/^\tint value() {$/;\"\tkind:function\tline:2\tnamespace:Test\tsignature:()\treturntype:int\n" + "setup\t" + quotedSketchLocation + "\t/^void setup() {}$/;\"\tkind:function\tline:7\tsignature:()\treturntype:void\n" + "loop\t" + quotedSketchLocation + "\t/^void loop() {}$/;\"\tkind:function\tline:8\tsignature:()\treturntype:void\n" - require.Equal(t, expectedOutput, strings.Replace(ctx.CTagsOutput, "\r\n", "\n", -1)) + require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1)) } func TestCTagsRunnerSketchWithTemplates(t *testing.T) { @@ -147,6 +150,7 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true + ctagsRunner := &builder.CTagsRunner{} commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, @@ -154,7 +158,7 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) { &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"}, - &builder.CTagsRunner{}, + ctagsRunner, } for _, command := range commands { err := command.Run(ctx) @@ -166,5 +170,5 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) { "bVar\t" + quotedSketchLocation + "\t/^c< 8 > bVar;$/;\"\tkind:variable\tline:15\n" + "aVar\t" + quotedSketchLocation + "\t/^c< 1<<8 > aVar;$/;\"\tkind:variable\tline:16\n" + "func\t" + quotedSketchLocation + "\t/^template func( c< 1< & aParam) {$/;\"\tkind:function\tline:18\tsignature:( c< 1< & aParam)\treturntype:template\n" - require.Equal(t, expectedOutput, strings.Replace(ctx.CTagsOutput, "\r\n", "\n", -1)) + require.Equal(t, expectedOutput, strings.Replace(string(ctagsRunner.CtagsOutput), "\r\n", "\n", -1)) } diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 0048d6d0c02..3c8473a8fef 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -115,7 +115,6 @@ type Context struct { UseCachedLibrariesResolution bool // C++ Parsing - CTagsOutput string CTagsTargetFile *paths.Path CTagsOfPreprocessedSource []*CTag LineOffset int From 298a55b1eaa7f73e53b078898c78250b4afe0fbd Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 30 Mar 2023 17:15:11 +0200 Subject: [PATCH 04/11] removing ctags-related data from builder ctx (part 2) --- legacy/builder/ctags/ctags_has_issues.go | 14 ++++++-------- legacy/builder/ctags_runner.go | 4 ++-- legacy/builder/types/context.go | 1 - 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/legacy/builder/ctags/ctags_has_issues.go b/legacy/builder/ctags/ctags_has_issues.go index 7cc693f2af6..a6cda249865 100644 --- a/legacy/builder/ctags/ctags_has_issues.go +++ b/legacy/builder/ctags/ctags_has_issues.go @@ -23,14 +23,12 @@ import ( "github.com/arduino/arduino-cli/legacy/builder/types" ) -func (p *CTagsParser) FixCLinkageTagsDeclarations(tags []*types.CTag) { - - linesMap := p.FindCLinkageLines(tags) - for i := range tags { - - if sliceContainsInt(linesMap[tags[i].Filename], tags[i].Line) && - !strings.Contains(tags[i].PrototypeModifiers, EXTERN) { - tags[i].PrototypeModifiers = tags[i].PrototypeModifiers + " " + EXTERN +func (p *CTagsParser) FixCLinkageTagsDeclarations() { + linesMap := p.FindCLinkageLines(p.tags) + for i := range p.tags { + if sliceContainsInt(linesMap[p.tags[i].Filename], p.tags[i].Line) && + !strings.Contains(p.tags[i].PrototypeModifiers, EXTERN) { + p.tags[i].PrototypeModifiers = p.tags[i].PrototypeModifiers + " " + EXTERN } } } diff --git a/legacy/builder/ctags_runner.go b/legacy/builder/ctags_runner.go index edf46a3d69a..dee108255e2 100644 --- a/legacy/builder/ctags_runner.go +++ b/legacy/builder/ctags_runner.go @@ -61,8 +61,8 @@ func (r *CTagsRunner) Run(ctx *types.Context) error { } parser := &ctags.CTagsParser{} - ctx.CTagsOfPreprocessedSource = parser.Parse(ctagsOutput, ctx.Sketch.MainFile) - parser.FixCLinkageTagsDeclarations(ctx.CTagsOfPreprocessedSource) + parser.Parse(ctagsOutput, ctx.Sketch.MainFile) + parser.FixCLinkageTagsDeclarations() protos, line := parser.GeneratePrototypes() if line != -1 { diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 3c8473a8fef..6349e2fce4d 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -116,7 +116,6 @@ type Context struct { // C++ Parsing CTagsTargetFile *paths.Path - CTagsOfPreprocessedSource []*CTag LineOffset int PrototypesSection string PrototypesLineWhereToInsert int From fea8dd9fe6d49d2db9d32c5fefe775e45018d623 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 30 Mar 2023 17:30:01 +0200 Subject: [PATCH 05/11] removing ctags-related data from builder ctx (part 3) --- legacy/builder/container_add_prototypes.go | 3 +-- legacy/builder/ctags_runner.go | 15 +++++++++++- legacy/builder/ctags_target_file_saver.go | 27 ---------------------- legacy/builder/test/ctags_runner_test.go | 15 ++++-------- legacy/builder/types/context.go | 1 - 5 files changed, 20 insertions(+), 41 deletions(-) diff --git a/legacy/builder/container_add_prototypes.go b/legacy/builder/container_add_prototypes.go index dd0ab219936..ce738e1ba26 100644 --- a/legacy/builder/container_add_prototypes.go +++ b/legacy/builder/container_add_prototypes.go @@ -55,8 +55,7 @@ func PreprocessSketchWithCtags(ctx *types.Context) error { commands := []types.Command{ &FilterSketchSource{Source: &ctx.SourceGccMinusE}, - &CTagsTargetFileSaver{Source: &ctx.SourceGccMinusE, TargetFileName: "ctags_target_for_gcc_minus_e.cpp"}, - &CTagsRunner{}, + &CTagsRunner{Source: &ctx.SourceGccMinusE, TargetFileName: "ctags_target_for_gcc_minus_e.cpp"}, &PrototypesAdder{}, } diff --git a/legacy/builder/ctags_runner.go b/legacy/builder/ctags_runner.go index dee108255e2..40ef36434d1 100644 --- a/legacy/builder/ctags_runner.go +++ b/legacy/builder/ctags_runner.go @@ -27,12 +27,25 @@ import ( ) type CTagsRunner struct { + Source *string + TargetFileName string + // Needed for unit-testing CtagsOutput []byte } func (r *CTagsRunner) Run(ctx *types.Context) error { - ctagsTargetFilePath := ctx.CTagsTargetFile + source := *r.Source + + preprocPath := ctx.PreprocPath + if err := preprocPath.MkdirAll(); err != nil { + return errors.WithStack(err) + } + + ctagsTargetFilePath := preprocPath.Join(r.TargetFileName) + if err := ctagsTargetFilePath.WriteFile([]byte(source)); err != nil { + return errors.WithStack(err) + } buildProperties := properties.NewMap() buildProperties.Set("tools.ctags.path", "{runtime.tools.ctags.path}") diff --git a/legacy/builder/ctags_target_file_saver.go b/legacy/builder/ctags_target_file_saver.go index a4014662c41..261bc7c759a 100644 --- a/legacy/builder/ctags_target_file_saver.go +++ b/legacy/builder/ctags_target_file_saver.go @@ -14,30 +14,3 @@ // To purchase a commercial license, send an email to license@arduino.cc. package builder - -import ( - "github.com/arduino/arduino-cli/legacy/builder/types" - "github.com/pkg/errors" -) - -type CTagsTargetFileSaver struct { - Source *string - TargetFileName string -} - -func (s *CTagsTargetFileSaver) Run(ctx *types.Context) error { - source := *s.Source - - preprocPath := ctx.PreprocPath - if err := preprocPath.MkdirAll(); err != nil { - return errors.WithStack(err) - } - - ctagsTargetFilePath := preprocPath.Join(s.TargetFileName) - if err := ctagsTargetFilePath.WriteFile([]byte(source)); err != nil { - return errors.WithStack(err) - } - - ctx.CTagsTargetFile = ctagsTargetFilePath - return nil -} diff --git a/legacy/builder/test/ctags_runner_test.go b/legacy/builder/test/ctags_runner_test.go index b5b9e823dc5..fe44735d66d 100644 --- a/legacy/builder/test/ctags_runner_test.go +++ b/legacy/builder/test/ctags_runner_test.go @@ -31,14 +31,13 @@ func TestCTagsRunner(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true - ctagsRunner := &builder.CTagsRunner{} + ctagsRunner := &builder.CTagsRunner{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"} commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, &builder.ContainerFindIncludes{}, &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, - &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"}, ctagsRunner, } for _, command := range commands { @@ -63,14 +62,13 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true - ctagsRunner := &builder.CTagsRunner{} + ctagsRunner := &builder.CTagsRunner{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"} commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, &builder.ContainerFindIncludes{}, &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, - &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"}, ctagsRunner, } for _, command := range commands { @@ -93,14 +91,13 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true - ctagsRunner := &builder.CTagsRunner{} + ctagsRunner := &builder.CTagsRunner{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"} commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, &builder.ContainerFindIncludes{}, &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, - &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"}, ctagsRunner, } for _, command := range commands { @@ -122,14 +119,13 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true - ctagsRunner := &builder.CTagsRunner{} + ctagsRunner := &builder.CTagsRunner{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"} commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, &builder.ContainerFindIncludes{}, &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, - &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"}, ctagsRunner, } for _, command := range commands { @@ -150,14 +146,13 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true - ctagsRunner := &builder.CTagsRunner{} + ctagsRunner := &builder.CTagsRunner{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"} commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, &builder.ContainerFindIncludes{}, &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, - &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"}, ctagsRunner, } for _, command := range commands { diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index 6349e2fce4d..b37cbe5a467 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -115,7 +115,6 @@ type Context struct { UseCachedLibrariesResolution bool // C++ Parsing - CTagsTargetFile *paths.Path LineOffset int PrototypesSection string PrototypesLineWhereToInsert int From 074027d8e284ad59eb01f00755171d1f42830994 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 31 Mar 2023 17:04:50 +0200 Subject: [PATCH 06/11] Clearly separate Source code processing phases --- legacy/builder/builder.go | 2 +- legacy/builder/container_add_prototypes.go | 10 ++-- .../container_merge_copy_sketch_files.go | 6 +-- legacy/builder/create_cmake_rule.go | 2 +- legacy/builder/preprocess_sketch.go | 11 ++--- legacy/builder/prototypes_adder.go | 9 ++-- legacy/builder/test/builder_test.go | 12 ++--- legacy/builder/test/ctags_runner_test.go | 10 ++-- legacy/builder/test/prototypes_adder_test.go | 46 +++++++++---------- legacy/builder/types/context.go | 13 ++++-- 10 files changed, 63 insertions(+), 58 deletions(-) diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index d73ec778ca7..b55df582310 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -148,7 +148,7 @@ func (s *Preprocess) Run(ctx *types.Context) error { } // Output arduino-preprocessed source - ctx.WriteStdout([]byte(ctx.Source)) + ctx.WriteStdout([]byte(ctx.SketchSourceAfterArduinoPreprocessing)) return nil } diff --git a/legacy/builder/container_add_prototypes.go b/legacy/builder/container_add_prototypes.go index ce738e1ba26..bf012ca1a6d 100644 --- a/legacy/builder/container_add_prototypes.go +++ b/legacy/builder/container_add_prototypes.go @@ -28,7 +28,7 @@ func PreprocessSketchWithCtags(ctx *types.Context) error { if err := ctx.PreprocPath.MkdirAll(); err != nil { return errors.WithStack(err) } - targetFilePath := ctx.PreprocPath.Join("ctags_target_for_gcc_minus_e.cpp") + targetFilePath := ctx.PreprocPath.Join("sketch_merged.cpp") // Run preprocessor sourceFile := ctx.SketchBuildPath.Join(ctx.Sketch.MainFile.Base() + ".cpp") @@ -50,12 +50,12 @@ func PreprocessSketchWithCtags(ctx *types.Context) error { if src, err := targetFilePath.ReadFile(); err != nil { return err } else { - ctx.SourceGccMinusE = string(src) + ctx.SketchSourceAfterCppPreprocessing = string(src) } commands := []types.Command{ - &FilterSketchSource{Source: &ctx.SourceGccMinusE}, - &CTagsRunner{Source: &ctx.SourceGccMinusE, TargetFileName: "ctags_target_for_gcc_minus_e.cpp"}, + &FilterSketchSource{Source: &ctx.SketchSourceAfterCppPreprocessing}, + &CTagsRunner{Source: &ctx.SketchSourceAfterCppPreprocessing, TargetFileName: "sketch_merged.cpp"}, &PrototypesAdder{}, } @@ -67,7 +67,7 @@ func PreprocessSketchWithCtags(ctx *types.Context) error { } } - if err := bldr.SketchSaveItemCpp(ctx.Sketch.MainFile, []byte(ctx.Source), ctx.SketchBuildPath); err != nil { + if err := bldr.SketchSaveItemCpp(ctx.Sketch.MainFile, []byte(ctx.SketchSourceAfterArduinoPreprocessing), ctx.SketchBuildPath); err != nil { return errors.WithStack(err) } diff --git a/legacy/builder/container_merge_copy_sketch_files.go b/legacy/builder/container_merge_copy_sketch_files.go index b98eaebb721..d0eeeaedd0a 100644 --- a/legacy/builder/container_merge_copy_sketch_files.go +++ b/legacy/builder/container_merge_copy_sketch_files.go @@ -24,14 +24,14 @@ import ( type ContainerMergeCopySketchFiles struct{} func (s *ContainerMergeCopySketchFiles) Run(ctx *types.Context) error { - offset, source, err := bldr.SketchMergeSources(ctx.Sketch, ctx.SourceOverride) + offset, mergedSource, err := bldr.SketchMergeSources(ctx.Sketch, ctx.SourceOverride) if err != nil { return err } ctx.LineOffset = offset - ctx.Source = source + ctx.SketchSourceMerged = mergedSource - if err := bldr.SketchSaveItemCpp(ctx.Sketch.MainFile, []byte(ctx.Source), ctx.SketchBuildPath); err != nil { + if err := bldr.SketchSaveItemCpp(ctx.Sketch.MainFile, []byte(mergedSource), ctx.SketchBuildPath); err != nil { return errors.WithStack(err) } diff --git a/legacy/builder/create_cmake_rule.go b/legacy/builder/create_cmake_rule.go index 16a9aa7781a..939a4f79c39 100644 --- a/legacy/builder/create_cmake_rule.go +++ b/legacy/builder/create_cmake_rule.go @@ -121,7 +121,7 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error { // Use old ctags method to generate export file commands := []types.Command{ - &FilterSketchSource{Source: &ctx.Source, RemoveLineMarkers: true}, + &FilterSketchSource{Source: &ctx.SketchSourceMerged, RemoveLineMarkers: true}, } for _, command := range commands { command.Run(ctx) diff --git a/legacy/builder/preprocess_sketch.go b/legacy/builder/preprocess_sketch.go index 31c19ca3bc1..12c2d8f38ca 100644 --- a/legacy/builder/preprocess_sketch.go +++ b/legacy/builder/preprocess_sketch.go @@ -35,9 +35,9 @@ func PreprocessSketchWithArduinoPreprocessor(ctx *types.Context) error { } sourceFile := ctx.SketchBuildPath.Join(ctx.Sketch.MainFile.Base() + ".cpp") - GCCPreprocRunner(ctx, sourceFile, ctx.PreprocPath.Join("ctags_target_for_gcc_minus_e.cpp"), ctx.IncludeFolders) + targetFile := ctx.PreprocPath.Join("sketch_merged.cpp") + GCCPreprocRunner(ctx, sourceFile, targetFile, ctx.IncludeFolders) - targetFilePath := ctx.PreprocPath.Join("ctags_target_for_gcc_minus_e.cpp") buildProperties := properties.NewMap() buildProperties.Set("tools.arduino-preprocessor.path", "{runtime.tools.arduino-preprocessor.path}") buildProperties.Set("tools.arduino-preprocessor.cmd.path", "{path}/arduino-preprocessor") @@ -45,7 +45,7 @@ func PreprocessSketchWithArduinoPreprocessor(ctx *types.Context) error { buildProperties.Set("preproc.macros.flags", "-w -x c++ -E -CC") buildProperties.Merge(ctx.BuildProperties) buildProperties.Merge(buildProperties.SubTree("tools").SubTree("arduino-preprocessor")) - buildProperties.SetPath("source_file", targetFilePath) + buildProperties.SetPath("source_file", targetFile) pattern := buildProperties.Get("pattern") if pattern == "" { @@ -79,7 +79,6 @@ func PreprocessSketchWithArduinoPreprocessor(ctx *types.Context) error { result := utils.NormalizeUTF8(buf) //fmt.Printf("PREPROCESSOR OUTPUT:\n%s\n", output) - ctx.Source = string(result) - - return bldr.SketchSaveItemCpp(ctx.Sketch.MainFile, []byte(ctx.Source), ctx.SketchBuildPath) + ctx.SketchSourceAfterArduinoPreprocessing = string(result) + return bldr.SketchSaveItemCpp(ctx.Sketch.MainFile, []byte(ctx.SketchSourceAfterArduinoPreprocessing), ctx.SketchBuildPath) } diff --git a/legacy/builder/prototypes_adder.go b/legacy/builder/prototypes_adder.go index abc816b2763..03a2cffef3a 100644 --- a/legacy/builder/prototypes_adder.go +++ b/legacy/builder/prototypes_adder.go @@ -17,19 +17,20 @@ package builder import ( "fmt" + "strconv" + "strings" + "github.com/arduino/arduino-cli/legacy/builder/constants" "github.com/arduino/arduino-cli/legacy/builder/types" "github.com/arduino/arduino-cli/legacy/builder/utils" - "strconv" - "strings" ) type PrototypesAdder struct{} func (s *PrototypesAdder) Run(ctx *types.Context) error { debugOutput := ctx.DebugPreprocessor - source := ctx.Source + source := ctx.SketchSourceMerged source = strings.Replace(source, "\r\n", "\n", -1) source = strings.Replace(source, "\r", "\n", -1) @@ -61,7 +62,7 @@ func (s *PrototypesAdder) Run(ctx *types.Context) error { } fmt.Println("#END OF PREPROCESSED SOURCE") } - ctx.Source = source + ctx.SketchSourceAfterArduinoPreprocessing = source return nil } diff --git a/legacy/builder/test/builder_test.go b/legacy/builder/test/builder_test.go index 8e088eb50c1..e84e2d402dd 100644 --- a/legacy/builder/test/builder_test.go +++ b/legacy/builder/test/builder_test.go @@ -116,7 +116,7 @@ func TestBuilderEmptySketch(t *testing.T) { exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck() NoError(t, err) require.True(t, exist) - exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck() + exist, err = buildPath.Join(constants.FOLDER_PREPROC, "sketch_merged.cpp").ExistCheck() NoError(t, err) require.True(t, exist) exist, err = buildPath.Join(constants.FOLDER_SKETCH, "sketch1.ino.cpp.o").ExistCheck() @@ -143,7 +143,7 @@ func TestBuilderBridge(t *testing.T) { exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck() NoError(t, err) require.True(t, exist) - exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck() + exist, err = buildPath.Join(constants.FOLDER_PREPROC, "sketch_merged.cpp").ExistCheck() NoError(t, err) require.True(t, exist) exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck() @@ -173,7 +173,7 @@ func TestBuilderSketchWithConfig(t *testing.T) { exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck() NoError(t, err) require.True(t, exist) - exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck() + exist, err = buildPath.Join(constants.FOLDER_PREPROC, "sketch_merged.cpp").ExistCheck() NoError(t, err) require.True(t, exist) exist, err = buildPath.Join(constants.FOLDER_SKETCH, "sketch_with_config.ino.cpp.o").ExistCheck() @@ -208,7 +208,7 @@ func TestBuilderBridgeTwice(t *testing.T) { exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck() NoError(t, err) require.True(t, exist) - exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck() + exist, err = buildPath.Join(constants.FOLDER_PREPROC, "sketch_merged.cpp").ExistCheck() NoError(t, err) require.True(t, exist) exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck() @@ -245,7 +245,7 @@ func TestBuilderBridgeSAM(t *testing.T) { exist, err = buildPath.Join(constants.FOLDER_CORE, "avr", "dtostrf.c.d").ExistCheck() NoError(t, err) require.True(t, exist) - exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck() + exist, err = buildPath.Join(constants.FOLDER_PREPROC, "sketch_merged.cpp").ExistCheck() NoError(t, err) require.True(t, exist) exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck() @@ -286,7 +286,7 @@ func TestBuilderBridgeRedBearLab(t *testing.T) { exist, err := buildPath.Join(constants.FOLDER_CORE, "HardwareSerial.cpp.o").ExistCheck() NoError(t, err) require.True(t, exist) - exist, err = buildPath.Join(constants.FOLDER_PREPROC, "ctags_target_for_gcc_minus_e.cpp").ExistCheck() + exist, err = buildPath.Join(constants.FOLDER_PREPROC, "sketch_merged.cpp").ExistCheck() NoError(t, err) require.True(t, exist) exist, err = buildPath.Join(constants.FOLDER_SKETCH, "Bridge.ino.cpp.o").ExistCheck() diff --git a/legacy/builder/test/ctags_runner_test.go b/legacy/builder/test/ctags_runner_test.go index fe44735d66d..d63a5314a49 100644 --- a/legacy/builder/test/ctags_runner_test.go +++ b/legacy/builder/test/ctags_runner_test.go @@ -31,7 +31,7 @@ func TestCTagsRunner(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true - ctagsRunner := &builder.CTagsRunner{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"} + ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"} commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, @@ -62,7 +62,7 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true - ctagsRunner := &builder.CTagsRunner{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"} + ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"} commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, @@ -91,7 +91,7 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true - ctagsRunner := &builder.CTagsRunner{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"} + ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"} commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, @@ -119,7 +119,7 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true - ctagsRunner := &builder.CTagsRunner{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"} + ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"} commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, @@ -146,7 +146,7 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true - ctagsRunner := &builder.CTagsRunner{Source: &ctx.Source, TargetFileName: "ctags_target.cpp"} + ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"} commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, &builder.ContainerMergeCopySketchFiles{}, diff --git a/legacy/builder/test/prototypes_adder_test.go b/legacy/builder/test/prototypes_adder_test.go index 67989677ea0..dbac210d086 100644 --- a/legacy/builder/test/prototypes_adder_test.go +++ b/legacy/builder/test/prototypes_adder_test.go @@ -48,7 +48,7 @@ func TestPrototypesAdderBridgeExample(t *testing.T) { } NoError(t, builder.PreprocessSketchWithCtags(ctx)) - require.Contains(t, ctx.Source, "#include \n#line 1 "+quotedSketchLocation+"\n") + require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include \n#line 1 "+quotedSketchLocation+"\n") 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) } @@ -70,7 +70,7 @@ func TestPrototypesAdderSketchWithIfDef(t *testing.T) { NoError(t, builder.PreprocessSketchWithCtags(ctx)) preprocessed := LoadAndInterpolate(t, filepath.Join("SketchWithIfDef", "SketchWithIfDef.preprocessed.txt"), ctx) - require.Equal(t, preprocessed, strings.Replace(ctx.Source, "\r\n", "\n", -1)) + require.Equal(t, preprocessed, strings.Replace(ctx.SketchSourceAfterArduinoPreprocessing, "\r\n", "\n", -1)) } func TestPrototypesAdderBaladuino(t *testing.T) { @@ -91,7 +91,7 @@ func TestPrototypesAdderBaladuino(t *testing.T) { NoError(t, builder.PreprocessSketchWithCtags(ctx)) preprocessed := LoadAndInterpolate(t, filepath.Join("Baladuino", "Baladuino.preprocessed.txt"), ctx) - require.Equal(t, preprocessed, strings.Replace(ctx.Source, "\r\n", "\n", -1)) + require.Equal(t, preprocessed, strings.Replace(ctx.SketchSourceAfterArduinoPreprocessing, "\r\n", "\n", -1)) } func TestPrototypesAdderCharWithEscapedDoubleQuote(t *testing.T) { @@ -112,7 +112,7 @@ func TestPrototypesAdderCharWithEscapedDoubleQuote(t *testing.T) { NoError(t, builder.PreprocessSketchWithCtags(ctx)) preprocessed := LoadAndInterpolate(t, filepath.Join("CharWithEscapedDoubleQuote", "CharWithEscapedDoubleQuote.preprocessed.txt"), ctx) - require.Equal(t, preprocessed, strings.Replace(ctx.Source, "\r\n", "\n", -1)) + require.Equal(t, preprocessed, strings.Replace(ctx.SketchSourceAfterArduinoPreprocessing, "\r\n", "\n", -1)) } func TestPrototypesAdderIncludeBetweenMultilineComment(t *testing.T) { @@ -133,7 +133,7 @@ func TestPrototypesAdderIncludeBetweenMultilineComment(t *testing.T) { NoError(t, builder.PreprocessSketchWithCtags(ctx)) preprocessed := LoadAndInterpolate(t, filepath.Join("IncludeBetweenMultilineComment", "IncludeBetweenMultilineComment.preprocessed.txt"), ctx) - require.Equal(t, preprocessed, strings.Replace(ctx.Source, "\r\n", "\n", -1)) + require.Equal(t, preprocessed, strings.Replace(ctx.SketchSourceAfterArduinoPreprocessing, "\r\n", "\n", -1)) } func TestPrototypesAdderLineContinuations(t *testing.T) { @@ -154,7 +154,7 @@ func TestPrototypesAdderLineContinuations(t *testing.T) { NoError(t, builder.PreprocessSketchWithCtags(ctx)) preprocessed := LoadAndInterpolate(t, filepath.Join("LineContinuations", "LineContinuations.preprocessed.txt"), ctx) - require.Equal(t, preprocessed, strings.Replace(ctx.Source, "\r\n", "\n", -1)) + require.Equal(t, preprocessed, strings.Replace(ctx.SketchSourceAfterArduinoPreprocessing, "\r\n", "\n", -1)) } func TestPrototypesAdderStringWithComment(t *testing.T) { @@ -175,7 +175,7 @@ func TestPrototypesAdderStringWithComment(t *testing.T) { NoError(t, builder.PreprocessSketchWithCtags(ctx)) preprocessed := LoadAndInterpolate(t, filepath.Join("StringWithComment", "StringWithComment.preprocessed.txt"), ctx) - require.Equal(t, preprocessed, strings.Replace(ctx.Source, "\r\n", "\n", -1)) + require.Equal(t, preprocessed, strings.Replace(ctx.SketchSourceAfterArduinoPreprocessing, "\r\n", "\n", -1)) } func TestPrototypesAdderSketchWithStruct(t *testing.T) { @@ -196,7 +196,7 @@ func TestPrototypesAdderSketchWithStruct(t *testing.T) { NoError(t, builder.PreprocessSketchWithCtags(ctx)) preprocessed := LoadAndInterpolate(t, filepath.Join("SketchWithStruct", "SketchWithStruct.preprocessed.txt"), ctx) - obtained := strings.Replace(ctx.Source, "\r\n", "\n", -1) + obtained := strings.Replace(ctx.SketchSourceAfterArduinoPreprocessing, "\r\n", "\n", -1) // ctags based preprocessing removes the space after "dostuff", but this is still OK // TODO: remove this exception when moving to a more powerful parser preprocessed = strings.Replace(preprocessed, "void dostuff (A_NEW_TYPE * bar);", "void dostuff(A_NEW_TYPE * bar);", 1) @@ -224,11 +224,11 @@ func TestPrototypesAdderSketchWithConfig(t *testing.T) { } NoError(t, builder.PreprocessSketchWithCtags(ctx)) - require.Contains(t, ctx.Source, "#include \n#line 1 "+quotedSketchLocation+"\n") + require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include \n#line 1 "+quotedSketchLocation+"\n") require.Equal(t, "#line 13 "+quotedSketchLocation+"\nvoid setup();\n#line 17 "+quotedSketchLocation+"\nvoid loop();\n#line 13 "+quotedSketchLocation+"\n", ctx.PrototypesSection) preprocessed := LoadAndInterpolate(t, filepath.Join("sketch_with_config", "sketch_with_config.preprocessed.txt"), ctx) - require.Equal(t, preprocessed, strings.Replace(ctx.Source, "\r\n", "\n", -1)) + require.Equal(t, preprocessed, strings.Replace(ctx.SketchSourceAfterArduinoPreprocessing, "\r\n", "\n", -1)) } func TestPrototypesAdderSketchNoFunctionsTwoFiles(t *testing.T) { @@ -251,7 +251,7 @@ func TestPrototypesAdderSketchNoFunctionsTwoFiles(t *testing.T) { } NoError(t, builder.PreprocessSketchWithCtags(ctx)) - require.Contains(t, ctx.Source, "#include \n#line 1 "+quotedSketchLocation+"\n") + require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include \n#line 1 "+quotedSketchLocation+"\n") require.Equal(t, "", ctx.PrototypesSection) } @@ -275,7 +275,7 @@ func TestPrototypesAdderSketchNoFunctions(t *testing.T) { } NoError(t, builder.PreprocessSketchWithCtags(ctx)) - require.Contains(t, ctx.Source, "#include \n#line 1 "+quotedSketchLocation+"\n") + require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include \n#line 1 "+quotedSketchLocation+"\n") require.Equal(t, "", ctx.PrototypesSection) } @@ -299,7 +299,7 @@ func TestPrototypesAdderSketchWithDefaultArgs(t *testing.T) { } NoError(t, builder.PreprocessSketchWithCtags(ctx)) - require.Contains(t, ctx.Source, "#include \n#line 1 "+quotedSketchLocation+"\n") + require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include \n#line 1 "+quotedSketchLocation+"\n") require.Equal(t, "#line 4 "+quotedSketchLocation+"\nvoid setup();\n#line 7 "+quotedSketchLocation+"\nvoid loop();\n#line 1 "+quotedSketchLocation+"\n", ctx.PrototypesSection) } @@ -323,7 +323,7 @@ func TestPrototypesAdderSketchWithInlineFunction(t *testing.T) { } NoError(t, builder.PreprocessSketchWithCtags(ctx)) - require.Contains(t, ctx.Source, "#include \n#line 1 "+quotedSketchLocation+"\n") + require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include \n#line 1 "+quotedSketchLocation+"\n") 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" obtained := ctx.PrototypesSection @@ -358,7 +358,7 @@ func TestPrototypesAdderSketchWithFunctionSignatureInsideIFDEF(t *testing.T) { } NoError(t, builder.PreprocessSketchWithCtags(ctx)) - require.Contains(t, ctx.Source, "#include \n#line 1 "+quotedSketchLocation+"\n") + require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include \n#line 1 "+quotedSketchLocation+"\n") 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) } @@ -387,7 +387,7 @@ func TestPrototypesAdderSketchWithUSBCON(t *testing.T) { } NoError(t, builder.PreprocessSketchWithCtags(ctx)) - require.Contains(t, ctx.Source, "#include \n#line 1 "+quotedSketchLocation+"\n") + require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include \n#line 1 "+quotedSketchLocation+"\n") 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) } @@ -415,7 +415,7 @@ func TestPrototypesAdderSketchWithTypename(t *testing.T) { } NoError(t, builder.PreprocessSketchWithCtags(ctx)) - require.Contains(t, ctx.Source, "#include \n#line 1 "+quotedSketchLocation+"\n") + require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include \n#line 1 "+quotedSketchLocation+"\n") expected := "#line 6 " + quotedSketchLocation + "\nvoid setup();\n#line 10 " + quotedSketchLocation + "\nvoid loop();\n#line 12 " + quotedSketchLocation + "\ntypename Foo::Bar func();\n#line 6 " + quotedSketchLocation + "\n" obtained := ctx.PrototypesSection // ctags based preprocessing ignores line with typename @@ -445,11 +445,11 @@ func TestPrototypesAdderSketchWithIfDef2(t *testing.T) { } NoError(t, builder.PreprocessSketchWithCtags(ctx)) - require.Contains(t, ctx.Source, "#include \n#line 1 "+quotedSketchLocation+"\n") + require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include \n#line 1 "+quotedSketchLocation+"\n") 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) expectedSource := LoadAndInterpolate(t, filepath.Join("sketch_with_ifdef", "sketch.preprocessed.txt"), ctx) - require.Equal(t, expectedSource, strings.Replace(ctx.Source, "\r\n", "\n", -1)) + require.Equal(t, expectedSource, strings.Replace(ctx.SketchSourceAfterArduinoPreprocessing, "\r\n", "\n", -1)) } func TestPrototypesAdderSketchWithIfDef2SAM(t *testing.T) { @@ -472,11 +472,11 @@ func TestPrototypesAdderSketchWithIfDef2SAM(t *testing.T) { } NoError(t, builder.PreprocessSketchWithCtags(ctx)) - require.Contains(t, ctx.Source, "#include \n#line 1 "+quotedSketchLocation+"\n") + require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include \n#line 1 "+quotedSketchLocation+"\n") 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) expectedSource := LoadAndInterpolate(t, filepath.Join("sketch_with_ifdef", "sketch.preprocessed.SAM.txt"), ctx) - require.Equal(t, expectedSource, strings.Replace(ctx.Source, "\r\n", "\n", -1)) + require.Equal(t, expectedSource, strings.Replace(ctx.SketchSourceAfterArduinoPreprocessing, "\r\n", "\n", -1)) } func TestPrototypesAdderSketchWithConst(t *testing.T) { @@ -499,7 +499,7 @@ func TestPrototypesAdderSketchWithConst(t *testing.T) { } NoError(t, builder.PreprocessSketchWithCtags(ctx)) - require.Contains(t, ctx.Source, "#include \n#line 1 "+quotedSketchLocation+"\n") + require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "#include \n#line 1 "+quotedSketchLocation+"\n") 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) } @@ -542,5 +542,5 @@ func TestPrototypesAdderSketchWithSubstringFunctionMember(t *testing.T) { } NoError(t, builder.PreprocessSketchWithCtags(ctx)) - require.Contains(t, ctx.Source, "class Foo {\nint blooper(int x) { return x+1; }\n};\n\nFoo foo;\n\n#line 7 "+quotedSketchLocation+"\nvoid setup();") + require.Contains(t, ctx.SketchSourceAfterArduinoPreprocessing, "class Foo {\nint blooper(int x) { return x+1; }\n};\n\nFoo foo;\n\n#line 7 "+quotedSketchLocation+"\nvoid setup();") } diff --git a/legacy/builder/types/context.go b/legacy/builder/types/context.go index b37cbe5a467..895c5ef043f 100644 --- a/legacy/builder/types/context.go +++ b/legacy/builder/types/context.go @@ -100,12 +100,17 @@ type Context struct { CollectedSourceFiles *UniqueSourceFileQueue - Sketch *sketch.Sketch - Source string - SourceGccMinusE string - + Sketch *sketch.Sketch WarningsLevel string + // Arduino sketch (.ino) to C++ (.cpp) conversion steps: + // 1. Concatenate *.ino files into a single merged source file -> SketchSourceMerged + SketchSourceMerged string + // 2. Run a pass of C++ preprocessor to remove macro definitions and ifdef-ed code -> SketchSourceAfterCppPreprocessing + SketchSourceAfterCppPreprocessing string + // 3. Do the Arduino preprocessing of the sketch (add missing prototypes) -> SketchSourceAfterArduinoPreprocessing + SketchSourceAfterArduinoPreprocessing string + // Libraries handling LibrariesManager *librariesmanager.LibrariesManager LibrariesResolver *librariesresolver.Cpp From 9bdf0edcf3833957f15f9fe9af2316ffa3ad5913 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 31 Mar 2023 19:57:24 +0200 Subject: [PATCH 07/11] Removed Command wrapper from ContainerMergeCopySketchFiles --- legacy/builder/builder.go | 12 +- .../container_merge_copy_sketch_files.go | 28 ++-- legacy/builder/test/ctags_runner_test.go | 30 ++++- .../test/includes_to_include_folders_test.go | 48 +++++-- legacy/builder/test/prototypes_adder_test.go | 127 +++++++++++++++--- legacy/builder/types/types.go | 6 + 6 files changed, 196 insertions(+), 55 deletions(-) diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index b55df582310..3e03e00f14a 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -39,6 +39,7 @@ func (s *Builder) Run(ctx *types.Context) error { return err } + var _err error commands := []types.Command{ &ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -46,7 +47,10 @@ func (s *Builder) Run(ctx *types.Context) error { &RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.prebuild", Suffix: ".pattern"}, - &ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), utils.LogIfVerbose(false, tr("Detecting libraries used...")), &ContainerFindIncludes{}, @@ -127,6 +131,7 @@ func (s *Preprocess) Run(ctx *types.Context) error { return err } + var _err error commands := []types.Command{ &ContainerSetupHardwareToolsLibsSketchAndProps{}, @@ -134,7 +139,10 @@ func (s *Preprocess) Run(ctx *types.Context) error { &RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.prebuild", Suffix: ".pattern"}, - &ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &ContainerFindIncludes{}, diff --git a/legacy/builder/container_merge_copy_sketch_files.go b/legacy/builder/container_merge_copy_sketch_files.go index d0eeeaedd0a..4e1ac6dd2a8 100644 --- a/legacy/builder/container_merge_copy_sketch_files.go +++ b/legacy/builder/container_merge_copy_sketch_files.go @@ -17,27 +17,19 @@ package builder import ( bldr "github.com/arduino/arduino-cli/arduino/builder" - "github.com/arduino/arduino-cli/legacy/builder/types" - "github.com/pkg/errors" + "github.com/arduino/arduino-cli/arduino/sketch" + "github.com/arduino/go-paths-helper" ) -type ContainerMergeCopySketchFiles struct{} - -func (s *ContainerMergeCopySketchFiles) Run(ctx *types.Context) error { - offset, mergedSource, err := bldr.SketchMergeSources(ctx.Sketch, ctx.SourceOverride) - if err != nil { - return err +func CopySketchFilesToBuildPath(sketch *sketch.Sketch, sourceOverrides map[string]string, buildPath *paths.Path) (offset int, mergedSource string, err error) { + if offset, mergedSource, err = bldr.SketchMergeSources(sketch, sourceOverrides); err != nil { + return } - ctx.LineOffset = offset - ctx.SketchSourceMerged = mergedSource - - if err := bldr.SketchSaveItemCpp(ctx.Sketch.MainFile, []byte(mergedSource), ctx.SketchBuildPath); err != nil { - return errors.WithStack(err) + if err = bldr.SketchSaveItemCpp(sketch.MainFile, []byte(mergedSource), buildPath); err != nil { + return } - - if err := bldr.SketchCopyAdditionalFiles(ctx.Sketch, ctx.SketchBuildPath, ctx.SourceOverride); err != nil { - return errors.WithStack(err) + if err = bldr.SketchCopyAdditionalFiles(sketch, buildPath, sourceOverrides); err != nil { + return } - - return nil + return } diff --git a/legacy/builder/test/ctags_runner_test.go b/legacy/builder/test/ctags_runner_test.go index d63a5314a49..25bcd7147d8 100644 --- a/legacy/builder/test/ctags_runner_test.go +++ b/legacy/builder/test/ctags_runner_test.go @@ -32,9 +32,13 @@ func TestCTagsRunner(t *testing.T) { ctx.Verbose = true ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"} + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, @@ -63,9 +67,13 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) { ctx.Verbose = true ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"} + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, @@ -92,9 +100,13 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) { ctx.Verbose = true ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"} + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, @@ -120,9 +132,13 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) { ctx.Verbose = true ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"} + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, @@ -147,9 +163,13 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) { ctx.Verbose = true ctagsRunner := &builder.CTagsRunner{Source: &ctx.SketchSourceMerged, TargetFileName: "ctags_target.cpp"} + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, diff --git a/legacy/builder/test/includes_to_include_folders_test.go b/legacy/builder/test/includes_to_include_folders_test.go index 537b58d327e..a33400eb69b 100644 --- a/legacy/builder/test/includes_to_include_folders_test.go +++ b/legacy/builder/test/includes_to_include_folders_test.go @@ -31,9 +31,13 @@ func TestIncludesToIncludeFolders(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -51,9 +55,13 @@ func TestIncludesToIncludeFoldersSketchWithIfDef(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -70,9 +78,13 @@ func TestIncludesToIncludeFoldersIRremoteLibrary(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -92,9 +104,13 @@ func TestIncludesToIncludeFoldersANewLibrary(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -119,9 +135,13 @@ func TestIncludesToIncludeFoldersDuplicateLibs(t *testing.T) { ctx = prepareBuilderTestContext(t, ctx, paths.New("user_hardware", "my_avr_platform", "avr", "libraries", "SPI", "examples", "BarometricPressureSensor", "BarometricPressureSensor.ino"), "my_avr_platform:avr:custom_yun") defer cleanUpBuilderTestContext(t, ctx) + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -147,9 +167,13 @@ func TestIncludesToIncludeFoldersDuplicateLibsWithConflictingLibsOutsideOfPlatfo ctx = prepareBuilderTestContext(t, ctx, paths.New("user_hardware", "my_avr_platform", "avr", "libraries", "SPI", "examples", "BarometricPressureSensor", "BarometricPressureSensor.ino"), "my_avr_platform:avr:custom_yun") defer cleanUpBuilderTestContext(t, ctx) + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -175,9 +199,13 @@ func TestIncludesToIncludeFoldersDuplicateLibs2(t *testing.T) { ctx = prepareBuilderTestContext(t, ctx, paths.New("sketch_usbhost", "sketch_usbhost.ino"), "arduino:samd:arduino_zero_native") defer cleanUpBuilderTestContext(t, ctx) + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -197,9 +225,13 @@ func TestIncludesToIncludeFoldersSubfolders(t *testing.T) { defer cleanUpBuilderTestContext(t, ctx) ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { diff --git a/legacy/builder/test/prototypes_adder_test.go b/legacy/builder/test/prototypes_adder_test.go index dbac210d086..99d8c5ca7fc 100644 --- a/legacy/builder/test/prototypes_adder_test.go +++ b/legacy/builder/test/prototypes_adder_test.go @@ -37,9 +37,13 @@ func TestPrototypesAdderBridgeExample(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -58,9 +62,13 @@ func TestPrototypesAdderSketchWithIfDef(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -79,9 +87,13 @@ func TestPrototypesAdderBaladuino(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -100,9 +112,13 @@ func TestPrototypesAdderCharWithEscapedDoubleQuote(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -121,9 +137,13 @@ func TestPrototypesAdderIncludeBetweenMultilineComment(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -142,9 +162,13 @@ func TestPrototypesAdderLineContinuations(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -163,9 +187,13 @@ func TestPrototypesAdderStringWithComment(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -184,9 +212,13 @@ func TestPrototypesAdderSketchWithStruct(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -213,9 +245,13 @@ func TestPrototypesAdderSketchWithConfig(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -240,9 +276,13 @@ func TestPrototypesAdderSketchNoFunctionsTwoFiles(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -263,10 +303,13 @@ func TestPrototypesAdderSketchNoFunctions(t *testing.T) { sketchLocation := paths.New("sketch_no_functions", "sketch_no_functions.ino") quotedSketchLocation := utils.QuoteCppPath(Abs(t, sketchLocation)) - + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -288,9 +331,13 @@ func TestPrototypesAdderSketchWithDefaultArgs(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -312,9 +359,13 @@ func TestPrototypesAdderSketchWithInlineFunction(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -347,9 +398,13 @@ func TestPrototypesAdderSketchWithFunctionSignatureInsideIFDEF(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -376,9 +431,13 @@ func TestPrototypesAdderSketchWithUSBCON(t *testing.T) { ctx = prepareBuilderTestContext(t, ctx, sketchLocation, "arduino:avr:leonardo") defer cleanUpBuilderTestContext(t, ctx) + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -404,9 +463,13 @@ func TestPrototypesAdderSketchWithTypename(t *testing.T) { ctx = prepareBuilderTestContext(t, ctx, sketchLocation, "arduino:avr:leonardo") defer cleanUpBuilderTestContext(t, ctx) + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -434,9 +497,13 @@ func TestPrototypesAdderSketchWithIfDef2(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -461,9 +528,13 @@ func TestPrototypesAdderSketchWithIfDef2SAM(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -488,9 +559,13 @@ func TestPrototypesAdderSketchWithConst(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -509,9 +584,13 @@ func TestPrototypesAdderSketchWithDosEol(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { @@ -531,9 +610,13 @@ func TestPrototypesAdderSketchWithSubstringFunctionMember(t *testing.T) { ctx.Verbose = true + var _err error commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, - &builder.ContainerMergeCopySketchFiles{}, + types.BareCommand(func(ctx *types.Context) error { + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + return _err + }), &builder.ContainerFindIncludes{}, } for _, command := range commands { diff --git a/legacy/builder/types/types.go b/legacy/builder/types/types.go index 9640d8c0c7a..3a398aabda4 100644 --- a/legacy/builder/types/types.go +++ b/legacy/builder/types/types.go @@ -122,3 +122,9 @@ type CTag struct { type Command interface { Run(ctx *Context) error } + +type BareCommand func(ctx *Context) error + +func (cmd BareCommand) Run(ctx *Context) error { + return cmd(ctx) +} From 3586c6da761c5c2b2ceea6737be76e14c85248ad Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 3 Apr 2023 10:28:08 +0200 Subject: [PATCH 08/11] Moved builder.CopySketchFilesToBuildPath into proper location --- arduino/builder/sketch.go | 35 +++++++++++---- arduino/builder/sketch_test.go | 13 +++--- legacy/builder/builder.go | 5 ++- .../container_merge_copy_sketch_files.go | 35 --------------- legacy/builder/test/ctags_runner_test.go | 11 ++--- .../test/includes_to_include_folders_test.go | 17 ++++---- legacy/builder/test/prototypes_adder_test.go | 43 ++++++++++--------- 7 files changed, 72 insertions(+), 87 deletions(-) delete mode 100644 legacy/builder/container_merge_copy_sketch_files.go diff --git a/arduino/builder/sketch.go b/arduino/builder/sketch.go index b5a6a97abb9..f8e8039e58a 100644 --- a/arduino/builder/sketch.go +++ b/arduino/builder/sketch.go @@ -34,15 +34,31 @@ var ( tr = i18n.Tr ) -// QuoteCppString returns the given string as a quoted string for use with the C +// quoteCppString returns the given string as a quoted string for use with the C // preprocessor. This adds double quotes around it and escapes any // double quotes and backslashes in the string. -func QuoteCppString(str string) string { +func quoteCppString(str string) string { str = strings.Replace(str, "\\", "\\\\", -1) str = strings.Replace(str, "\"", "\\\"", -1) return "\"" + str + "\"" } +// PrepareSketchBuildPath copies the sketch source files in the build path. +// The .ino files are merged together to create a .cpp file (by the way, the +// .cpp file still needs to be Arduino-preprocessed to compile). +func PrepareSketchBuildPath(sketch *sketch.Sketch, sourceOverrides map[string]string, buildPath *paths.Path) (offset int, mergedSource string, err error) { + if offset, mergedSource, err = sketchMergeSources(sketch, sourceOverrides); err != nil { + return + } + if err = SketchSaveItemCpp(sketch.MainFile, []byte(mergedSource), buildPath); err != nil { + return + } + if err = sketchCopyAdditionalFiles(sketch, buildPath, sourceOverrides); err != nil { + return + } + return +} + // SketchSaveItemCpp saves a preprocessed .cpp sketch file on disk func SketchSaveItemCpp(path *paths.Path, contents []byte, destPath *paths.Path) error { sketchName := path.Base() @@ -59,8 +75,9 @@ func SketchSaveItemCpp(path *paths.Path, contents []byte, destPath *paths.Path) return nil } -// SketchMergeSources merges all the source files included in a sketch -func SketchMergeSources(sk *sketch.Sketch, overrides map[string]string) (int, string, error) { +// sketchMergeSources merges all the .ino source files included in a sketch to produce +// a single .cpp file. +func sketchMergeSources(sk *sketch.Sketch, overrides map[string]string) (int, string, error) { lineOffset := 0 mergedSource := "" @@ -89,7 +106,7 @@ func SketchMergeSources(sk *sketch.Sketch, overrides map[string]string) (int, st lineOffset++ } - mergedSource += "#line 1 " + QuoteCppString(sk.MainFile.String()) + "\n" + mergedSource += "#line 1 " + quoteCppString(sk.MainFile.String()) + "\n" mergedSource += mainSrc + "\n" lineOffset++ @@ -98,16 +115,16 @@ func SketchMergeSources(sk *sketch.Sketch, overrides map[string]string) (int, st if err != nil { return 0, "", err } - mergedSource += "#line 1 " + QuoteCppString(file.String()) + "\n" + mergedSource += "#line 1 " + quoteCppString(file.String()) + "\n" mergedSource += src + "\n" } return lineOffset, mergedSource, nil } -// SketchCopyAdditionalFiles copies the additional files for a sketch to the +// sketchCopyAdditionalFiles copies the additional files for a sketch to the // specified destination directory. -func SketchCopyAdditionalFiles(sketch *sketch.Sketch, destPath *paths.Path, overrides map[string]string) error { +func sketchCopyAdditionalFiles(sketch *sketch.Sketch, destPath *paths.Path, overrides map[string]string) error { if err := destPath.MkdirAll(); err != nil { return errors.Wrap(err, tr("unable to create a folder to save the sketch files")) } @@ -138,7 +155,7 @@ func SketchCopyAdditionalFiles(sketch *sketch.Sketch, destPath *paths.Path, over } // tag each addtional file with the filename of the source it was copied from - sourceBytes = append([]byte("#line 1 "+QuoteCppString(file.String())+"\n"), sourceBytes...) + sourceBytes = append([]byte("#line 1 "+quoteCppString(file.String())+"\n"), sourceBytes...) err = writeIfDifferent(sourceBytes, targetPath) if err != nil { diff --git a/arduino/builder/sketch_test.go b/arduino/builder/sketch_test.go index a8f1cb6b1b0..5466cdff819 100644 --- a/arduino/builder/sketch_test.go +++ b/arduino/builder/sketch_test.go @@ -13,7 +13,7 @@ // Arduino software without disclosing the source code of your own applications. // To purchase a commercial license, send an email to license@arduino.cc. -package builder_test +package builder import ( "fmt" @@ -23,7 +23,6 @@ import ( "strings" "testing" - "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" @@ -48,7 +47,7 @@ func TestSaveSketch(t *testing.T) { t.Fatalf("unable to read golden file %s: %v", sketchFile, err) } - builder.SketchSaveItemCpp(paths.New(sketchName), source, tmp) + SketchSaveItemCpp(paths.New(sketchName), source, tmp) out, err := tmp.Join(outName).ReadFile() if err != nil { @@ -82,7 +81,7 @@ func TestMergeSketchSources(t *testing.T) { } mergedSources := strings.ReplaceAll(string(mergedBytes), "%s", pathToGoldenSource) - offset, source, err := builder.SketchMergeSources(s, nil) + offset, source, err := sketchMergeSources(s, nil) require.Nil(t, err) require.Equal(t, 2, offset) require.Equal(t, mergedSources, source) @@ -94,7 +93,7 @@ func TestMergeSketchSourcesArduinoIncluded(t *testing.T) { require.NotNil(t, s) // ensure not to include Arduino.h when it's already there - _, source, err := builder.SketchMergeSources(s, nil) + _, source, err := sketchMergeSources(s, nil) require.Nil(t, err) require.Equal(t, 1, strings.Count(source, "")) } @@ -110,7 +109,7 @@ func TestCopyAdditionalFiles(t *testing.T) { // copy the sketch over, create a fake main file we don't care about it // but we need it for `SketchLoad` to succeed later - err = builder.SketchCopyAdditionalFiles(s1, tmp, nil) + err = sketchCopyAdditionalFiles(s1, tmp, nil) require.Nil(t, err) fakeIno := tmp.Join(fmt.Sprintf("%s.ino", tmp.Base())) require.Nil(t, fakeIno.WriteFile([]byte{})) @@ -125,7 +124,7 @@ func TestCopyAdditionalFiles(t *testing.T) { require.Nil(t, err) // copy again - err = builder.SketchCopyAdditionalFiles(s1, tmp, nil) + err = sketchCopyAdditionalFiles(s1, tmp, nil) require.Nil(t, err) // verify file hasn't changed diff --git a/legacy/builder/builder.go b/legacy/builder/builder.go index 3e03e00f14a..b1b1f007c5d 100644 --- a/legacy/builder/builder.go +++ b/legacy/builder/builder.go @@ -19,6 +19,7 @@ import ( "reflect" "time" + "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/i18n" "github.com/arduino/arduino-cli/legacy/builder/phases" "github.com/arduino/arduino-cli/legacy/builder/types" @@ -48,7 +49,7 @@ func (s *Builder) Run(ctx *types.Context) error { &RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.prebuild", Suffix: ".pattern"}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), @@ -140,7 +141,7 @@ func (s *Preprocess) Run(ctx *types.Context) error { &RecipeByPrefixSuffixRunner{Prefix: "recipe.hooks.prebuild", Suffix: ".pattern"}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), diff --git a/legacy/builder/container_merge_copy_sketch_files.go b/legacy/builder/container_merge_copy_sketch_files.go deleted file mode 100644 index 4e1ac6dd2a8..00000000000 --- a/legacy/builder/container_merge_copy_sketch_files.go +++ /dev/null @@ -1,35 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package builder - -import ( - bldr "github.com/arduino/arduino-cli/arduino/builder" - "github.com/arduino/arduino-cli/arduino/sketch" - "github.com/arduino/go-paths-helper" -) - -func CopySketchFilesToBuildPath(sketch *sketch.Sketch, sourceOverrides map[string]string, buildPath *paths.Path) (offset int, mergedSource string, err error) { - if offset, mergedSource, err = bldr.SketchMergeSources(sketch, sourceOverrides); err != nil { - return - } - if err = bldr.SketchSaveItemCpp(sketch.MainFile, []byte(mergedSource), buildPath); err != nil { - return - } - if err = bldr.SketchCopyAdditionalFiles(sketch, buildPath, sourceOverrides); err != nil { - return - } - return -} diff --git a/legacy/builder/test/ctags_runner_test.go b/legacy/builder/test/ctags_runner_test.go index 25bcd7147d8..12194ccf744 100644 --- a/legacy/builder/test/ctags_runner_test.go +++ b/legacy/builder/test/ctags_runner_test.go @@ -19,6 +19,7 @@ import ( "strings" "testing" + bldr "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/legacy/builder" "github.com/arduino/arduino-cli/legacy/builder/types" paths "github.com/arduino/go-paths-helper" @@ -36,7 +37,7 @@ func TestCTagsRunner(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -71,7 +72,7 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -104,7 +105,7 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -136,7 +137,7 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -167,7 +168,7 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, diff --git a/legacy/builder/test/includes_to_include_folders_test.go b/legacy/builder/test/includes_to_include_folders_test.go index a33400eb69b..a19d0ad57f2 100644 --- a/legacy/builder/test/includes_to_include_folders_test.go +++ b/legacy/builder/test/includes_to_include_folders_test.go @@ -20,6 +20,7 @@ import ( "sort" "testing" + bldr "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/legacy/builder" "github.com/arduino/arduino-cli/legacy/builder/types" paths "github.com/arduino/go-paths-helper" @@ -35,7 +36,7 @@ func TestIncludesToIncludeFolders(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -59,7 +60,7 @@ func TestIncludesToIncludeFoldersSketchWithIfDef(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -82,7 +83,7 @@ func TestIncludesToIncludeFoldersIRremoteLibrary(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -108,7 +109,7 @@ func TestIncludesToIncludeFoldersANewLibrary(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -139,7 +140,7 @@ func TestIncludesToIncludeFoldersDuplicateLibs(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -171,7 +172,7 @@ func TestIncludesToIncludeFoldersDuplicateLibsWithConflictingLibsOutsideOfPlatfo commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -203,7 +204,7 @@ func TestIncludesToIncludeFoldersDuplicateLibs2(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -229,7 +230,7 @@ func TestIncludesToIncludeFoldersSubfolders(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, diff --git a/legacy/builder/test/prototypes_adder_test.go b/legacy/builder/test/prototypes_adder_test.go index 99d8c5ca7fc..947f5bdbf02 100644 --- a/legacy/builder/test/prototypes_adder_test.go +++ b/legacy/builder/test/prototypes_adder_test.go @@ -21,6 +21,7 @@ import ( "strings" "testing" + bldr "github.com/arduino/arduino-cli/arduino/builder" "github.com/arduino/arduino-cli/legacy/builder" "github.com/arduino/arduino-cli/legacy/builder/types" "github.com/arduino/arduino-cli/legacy/builder/utils" @@ -41,7 +42,7 @@ func TestPrototypesAdderBridgeExample(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -66,7 +67,7 @@ func TestPrototypesAdderSketchWithIfDef(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -91,7 +92,7 @@ func TestPrototypesAdderBaladuino(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -116,7 +117,7 @@ func TestPrototypesAdderCharWithEscapedDoubleQuote(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -141,7 +142,7 @@ func TestPrototypesAdderIncludeBetweenMultilineComment(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -166,7 +167,7 @@ func TestPrototypesAdderLineContinuations(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -191,7 +192,7 @@ func TestPrototypesAdderStringWithComment(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -216,7 +217,7 @@ func TestPrototypesAdderSketchWithStruct(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -249,7 +250,7 @@ func TestPrototypesAdderSketchWithConfig(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -280,7 +281,7 @@ func TestPrototypesAdderSketchNoFunctionsTwoFiles(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -307,7 +308,7 @@ func TestPrototypesAdderSketchNoFunctions(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -335,7 +336,7 @@ func TestPrototypesAdderSketchWithDefaultArgs(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -363,7 +364,7 @@ func TestPrototypesAdderSketchWithInlineFunction(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -402,7 +403,7 @@ func TestPrototypesAdderSketchWithFunctionSignatureInsideIFDEF(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -435,7 +436,7 @@ func TestPrototypesAdderSketchWithUSBCON(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -467,7 +468,7 @@ func TestPrototypesAdderSketchWithTypename(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -501,7 +502,7 @@ func TestPrototypesAdderSketchWithIfDef2(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -532,7 +533,7 @@ func TestPrototypesAdderSketchWithIfDef2SAM(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -563,7 +564,7 @@ func TestPrototypesAdderSketchWithConst(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -588,7 +589,7 @@ func TestPrototypesAdderSketchWithDosEol(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, @@ -614,7 +615,7 @@ func TestPrototypesAdderSketchWithSubstringFunctionMember(t *testing.T) { commands := []types.Command{ &builder.ContainerSetupHardwareToolsLibsSketchAndProps{}, types.BareCommand(func(ctx *types.Context) error { - ctx.LineOffset, ctx.SketchSourceMerged, _err = builder.CopySketchFilesToBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) + ctx.LineOffset, ctx.SketchSourceMerged, _err = bldr.PrepareSketchBuildPath(ctx.Sketch, ctx.SourceOverride, ctx.SketchBuildPath) return _err }), &builder.ContainerFindIncludes{}, From 4cd74a3cdc5dda6d4672e354b398bd05dddfe210 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 3 Apr 2023 11:15:10 +0200 Subject: [PATCH 09/11] Converted FilterSketchSource into a function --- legacy/builder/container_add_prototypes.go | 4 ++-- legacy/builder/create_cmake_rule.go | 7 +------ legacy/builder/filter_sketch_source.go | 21 ++++++++------------- 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/legacy/builder/container_add_prototypes.go b/legacy/builder/container_add_prototypes.go index bf012ca1a6d..7fae47a86c8 100644 --- a/legacy/builder/container_add_prototypes.go +++ b/legacy/builder/container_add_prototypes.go @@ -16,6 +16,7 @@ package builder import ( + "bytes" "fmt" bldr "github.com/arduino/arduino-cli/arduino/builder" @@ -50,11 +51,10 @@ func PreprocessSketchWithCtags(ctx *types.Context) error { if src, err := targetFilePath.ReadFile(); err != nil { return err } else { - ctx.SketchSourceAfterCppPreprocessing = string(src) + ctx.SketchSourceAfterCppPreprocessing = FilterSketchSource(ctx.Sketch, bytes.NewReader(src), false) } commands := []types.Command{ - &FilterSketchSource{Source: &ctx.SketchSourceAfterCppPreprocessing}, &CTagsRunner{Source: &ctx.SketchSourceAfterCppPreprocessing, TargetFileName: "sketch_merged.cpp"}, &PrototypesAdder{}, } diff --git a/legacy/builder/create_cmake_rule.go b/legacy/builder/create_cmake_rule.go index 939a4f79c39..f3677a6d5df 100644 --- a/legacy/builder/create_cmake_rule.go +++ b/legacy/builder/create_cmake_rule.go @@ -120,12 +120,7 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error { } // Use old ctags method to generate export file - commands := []types.Command{ - &FilterSketchSource{Source: &ctx.SketchSourceMerged, RemoveLineMarkers: true}, - } - for _, command := range commands { - command.Run(ctx) - } + ctx.SketchSourceMerged = FilterSketchSource(ctx.Sketch, strings.NewReader(ctx.SketchSourceMerged), true) err = utils.CopyDir(ctx.SketchBuildPath.String(), cmakeFolder.Join("sketch").String(), validExportExtensions) if err != nil { diff --git a/legacy/builder/filter_sketch_source.go b/legacy/builder/filter_sketch_source.go index 507615a55b2..ef7a581b3da 100644 --- a/legacy/builder/filter_sketch_source.go +++ b/legacy/builder/filter_sketch_source.go @@ -17,34 +17,30 @@ package builder import ( "bufio" + "io" "strconv" "strings" "github.com/arduino/go-paths-helper" - "github.com/arduino/arduino-cli/legacy/builder/types" + "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/legacy/builder/utils" ) -type FilterSketchSource struct { - Source *string - RemoveLineMarkers bool -} - -func (s *FilterSketchSource) Run(ctx *types.Context) error { +func FilterSketchSource(sketch *sketch.Sketch, source io.Reader, removeLineMarkers bool) string { fileNames := paths.NewPathList() - fileNames.Add(ctx.Sketch.MainFile) - fileNames.AddAll(ctx.Sketch.OtherSketchFiles) + fileNames.Add(sketch.MainFile) + fileNames.AddAll(sketch.OtherSketchFiles) inSketch := false filtered := "" - scanner := bufio.NewScanner(strings.NewReader(*s.Source)) + scanner := bufio.NewScanner(source) for scanner.Scan() { line := scanner.Text() if filename := parseLineMarker(line); filename != nil { inSketch = fileNames.Contains(filename) - if inSketch && s.RemoveLineMarkers { + if inSketch && removeLineMarkers { continue } } @@ -54,8 +50,7 @@ func (s *FilterSketchSource) Run(ctx *types.Context) error { } } - *s.Source = filtered - return nil + return filtered } // Parses the given line as a gcc line marker and returns the contained From 5c1bff5ff3002165bb401d9e3518ad3adb17de54 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 3 Apr 2023 11:26:06 +0200 Subject: [PATCH 10/11] Moved a couple of functions in the proper builder package --- arduino/builder/cpp.go | 74 +++++++++++++++++++ arduino/builder/cpp_test.go | 46 ++++++++++++ arduino/builder/sketch.go | 16 +--- legacy/builder/container_add_prototypes.go | 65 ++++++++++++++++- legacy/builder/create_cmake_rule.go | 2 +- legacy/builder/filter_sketch_source.go | 85 ---------------------- legacy/builder/test/utils_test.go | 38 ---------- legacy/builder/utils/utils.go | 45 ------------ 8 files changed, 188 insertions(+), 183 deletions(-) create mode 100644 arduino/builder/cpp.go create mode 100644 arduino/builder/cpp_test.go delete mode 100644 legacy/builder/filter_sketch_source.go diff --git a/arduino/builder/cpp.go b/arduino/builder/cpp.go new file mode 100644 index 00000000000..1054388f43f --- /dev/null +++ b/arduino/builder/cpp.go @@ -0,0 +1,74 @@ +// This file is part of arduino-cli. +// +// Copyright 2023 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package builder + +import ( + "strings" + "unicode/utf8" +) + +// QuoteCppString returns the given string as a quoted string for use with the C +// preprocessor. This adds double quotes around it and escapes any +// double quotes and backslashes in the string. +func QuoteCppString(str string) string { + str = strings.Replace(str, "\\", "\\\\", -1) + str = strings.Replace(str, "\"", "\\\"", -1) + return "\"" + str + "\"" +} + +// Parse a C-preprocessor string as emitted by the preprocessor. This +// is a string contained in double quotes, with any backslashes or +// quotes escaped with a backslash. If a valid string was present at the +// start of the given line, returns the unquoted string contents, the +// remainder of the line (everything after the closing "), and true. +// Otherwise, returns the empty string, the entire line and false. +func ParseCppString(line string) (string, string, bool) { + // For details about how these strings are output by gcc, see: + // https://github.com/gcc-mirror/gcc/blob/a588355ab948cf551bc9d2b89f18e5ae5140f52c/libcpp/macro.c#L491-L511 + // Note that the documentation suggests all non-printable + // characters are also escaped, but the implementation does not + // actually do this. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51259 + if len(line) < 1 || line[0] != '"' { + return "", line, false + } + + i := 1 + res := "" + for { + if i >= len(line) { + return "", line, false + } + + c, width := utf8.DecodeRuneInString(line[i:]) + + switch c { + case '\\': + // Backslash, next character is used unmodified + i += width + if i >= len(line) { + return "", line, false + } + res += string(line[i]) + case '"': + // Quote, end of string + return res, line[i+width:], true + default: + res += string(c) + } + + i += width + } +} diff --git a/arduino/builder/cpp_test.go b/arduino/builder/cpp_test.go new file mode 100644 index 00000000000..84de5f0b254 --- /dev/null +++ b/arduino/builder/cpp_test.go @@ -0,0 +1,46 @@ +package builder_test + +import ( + "testing" + + "github.com/arduino/arduino-cli/arduino/builder" + "github.com/stretchr/testify/require" +) + +func TestParseCppString(t *testing.T) { + _, _, ok := builder.ParseCppString(`foo`) + require.Equal(t, false, ok) + + _, _, ok = builder.ParseCppString(`"foo`) + require.Equal(t, false, ok) + + str, rest, ok := builder.ParseCppString(`"foo"`) + require.Equal(t, true, ok) + require.Equal(t, `foo`, str) + require.Equal(t, ``, rest) + + str, rest, ok = builder.ParseCppString(`"foo\\bar"`) + require.Equal(t, true, ok) + require.Equal(t, `foo\bar`, str) + require.Equal(t, ``, rest) + + str, rest, ok = builder.ParseCppString(`"foo \"is\" quoted and \\\\bar\"\" escaped\\" and "then" some`) + require.Equal(t, true, ok) + require.Equal(t, `foo "is" quoted and \\bar"" escaped\`, str) + require.Equal(t, ` and "then" some`, rest) + + str, rest, ok = builder.ParseCppString(`" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz{|}~"`) + require.Equal(t, true, ok) + require.Equal(t, ` !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~`, str) + require.Equal(t, ``, rest) + + str, rest, ok = builder.ParseCppString(`"/home/ççç/"`) + require.Equal(t, true, ok) + require.Equal(t, `/home/ççç/`, str) + require.Equal(t, ``, rest) + + str, rest, ok = builder.ParseCppString(`"/home/ççç/ /$sdsdd\\"`) + require.Equal(t, true, ok) + require.Equal(t, `/home/ççç/ /$sdsdd\`, str) + require.Equal(t, ``, rest) +} diff --git a/arduino/builder/sketch.go b/arduino/builder/sketch.go index f8e8039e58a..8560178a193 100644 --- a/arduino/builder/sketch.go +++ b/arduino/builder/sketch.go @@ -19,7 +19,6 @@ import ( "bytes" "fmt" "regexp" - "strings" "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/i18n" @@ -34,15 +33,6 @@ var ( tr = i18n.Tr ) -// quoteCppString returns the given string as a quoted string for use with the C -// preprocessor. This adds double quotes around it and escapes any -// double quotes and backslashes in the string. -func quoteCppString(str string) string { - str = strings.Replace(str, "\\", "\\\\", -1) - str = strings.Replace(str, "\"", "\\\"", -1) - return "\"" + str + "\"" -} - // PrepareSketchBuildPath copies the sketch source files in the build path. // The .ino files are merged together to create a .cpp file (by the way, the // .cpp file still needs to be Arduino-preprocessed to compile). @@ -106,7 +96,7 @@ func sketchMergeSources(sk *sketch.Sketch, overrides map[string]string) (int, st lineOffset++ } - mergedSource += "#line 1 " + quoteCppString(sk.MainFile.String()) + "\n" + mergedSource += "#line 1 " + QuoteCppString(sk.MainFile.String()) + "\n" mergedSource += mainSrc + "\n" lineOffset++ @@ -115,7 +105,7 @@ func sketchMergeSources(sk *sketch.Sketch, overrides map[string]string) (int, st if err != nil { return 0, "", err } - mergedSource += "#line 1 " + quoteCppString(file.String()) + "\n" + mergedSource += "#line 1 " + QuoteCppString(file.String()) + "\n" mergedSource += src + "\n" } @@ -155,7 +145,7 @@ func sketchCopyAdditionalFiles(sketch *sketch.Sketch, destPath *paths.Path, over } // tag each addtional file with the filename of the source it was copied from - sourceBytes = append([]byte("#line 1 "+quoteCppString(file.String())+"\n"), sourceBytes...) + sourceBytes = append([]byte("#line 1 "+QuoteCppString(file.String())+"\n"), sourceBytes...) err = writeIfDifferent(sourceBytes, targetPath) if err != nil { diff --git a/legacy/builder/container_add_prototypes.go b/legacy/builder/container_add_prototypes.go index 7fae47a86c8..fe5e9064742 100644 --- a/legacy/builder/container_add_prototypes.go +++ b/legacy/builder/container_add_prototypes.go @@ -16,11 +16,17 @@ package builder import ( + "bufio" "bytes" "fmt" + "io" + "strconv" + "strings" bldr "github.com/arduino/arduino-cli/arduino/builder" + "github.com/arduino/arduino-cli/arduino/sketch" "github.com/arduino/arduino-cli/legacy/builder/types" + "github.com/arduino/go-paths-helper" "github.com/pkg/errors" ) @@ -51,7 +57,7 @@ func PreprocessSketchWithCtags(ctx *types.Context) error { if src, err := targetFilePath.ReadFile(); err != nil { return err } else { - ctx.SketchSourceAfterCppPreprocessing = FilterSketchSource(ctx.Sketch, bytes.NewReader(src), false) + ctx.SketchSourceAfterCppPreprocessing = filterSketchSource(ctx.Sketch, bytes.NewReader(src), false) } commands := []types.Command{ @@ -73,3 +79,60 @@ func PreprocessSketchWithCtags(ctx *types.Context) error { return nil } + +func filterSketchSource(sketch *sketch.Sketch, source io.Reader, removeLineMarkers bool) string { + fileNames := paths.NewPathList() + fileNames.Add(sketch.MainFile) + fileNames.AddAll(sketch.OtherSketchFiles) + + inSketch := false + filtered := "" + + scanner := bufio.NewScanner(source) + for scanner.Scan() { + line := scanner.Text() + if filename := parseLineMarker(line); filename != nil { + inSketch = fileNames.Contains(filename) + if inSketch && removeLineMarkers { + continue + } + } + + if inSketch { + filtered += line + "\n" + } + } + + return filtered +} + +// Parses the given line as a gcc line marker and returns the contained +// filename. +func parseLineMarker(line string) *paths.Path { + // A line marker contains the line number and filename and looks like: + // # 123 /path/to/file.cpp + // It can be followed by zero or more flag number that indicate the + // preprocessor state and can be ignored. + // For exact details on this format, see: + // https://github.com/gcc-mirror/gcc/blob/edd716b6b1caa1a5cb320a8cd7f626f30198e098/gcc/c-family/c-ppoutput.c#L413-L415 + + split := strings.SplitN(line, " ", 3) + if len(split) < 3 || len(split[0]) == 0 || split[0][0] != '#' { + return nil + } + + _, err := strconv.Atoi(split[1]) + if err != nil { + return nil + } + + // If we get here, we found a # followed by a line number, so + // assume this is a line marker and see if the rest of the line + // starts with a string containing the filename + str, rest, ok := bldr.ParseCppString(split[2]) + + if ok && (rest == "" || rest[0] == ' ') { + return paths.New(str) + } + return nil +} diff --git a/legacy/builder/create_cmake_rule.go b/legacy/builder/create_cmake_rule.go index f3677a6d5df..03549563780 100644 --- a/legacy/builder/create_cmake_rule.go +++ b/legacy/builder/create_cmake_rule.go @@ -120,7 +120,7 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error { } // Use old ctags method to generate export file - ctx.SketchSourceMerged = FilterSketchSource(ctx.Sketch, strings.NewReader(ctx.SketchSourceMerged), true) + ctx.SketchSourceMerged = filterSketchSource(ctx.Sketch, strings.NewReader(ctx.SketchSourceMerged), true) err = utils.CopyDir(ctx.SketchBuildPath.String(), cmakeFolder.Join("sketch").String(), validExportExtensions) if err != nil { diff --git a/legacy/builder/filter_sketch_source.go b/legacy/builder/filter_sketch_source.go deleted file mode 100644 index ef7a581b3da..00000000000 --- a/legacy/builder/filter_sketch_source.go +++ /dev/null @@ -1,85 +0,0 @@ -// This file is part of arduino-cli. -// -// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) -// -// This software is released under the GNU General Public License version 3, -// which covers the main part of arduino-cli. -// The terms of this license can be found at: -// https://www.gnu.org/licenses/gpl-3.0.en.html -// -// You can be released from the requirements of the above licenses by purchasing -// a commercial license. Buying such a license is mandatory if you want to -// modify or otherwise use the software for commercial activities involving the -// Arduino software without disclosing the source code of your own applications. -// To purchase a commercial license, send an email to license@arduino.cc. - -package builder - -import ( - "bufio" - "io" - "strconv" - "strings" - - "github.com/arduino/go-paths-helper" - - "github.com/arduino/arduino-cli/arduino/sketch" - "github.com/arduino/arduino-cli/legacy/builder/utils" -) - -func FilterSketchSource(sketch *sketch.Sketch, source io.Reader, removeLineMarkers bool) string { - fileNames := paths.NewPathList() - fileNames.Add(sketch.MainFile) - fileNames.AddAll(sketch.OtherSketchFiles) - - inSketch := false - filtered := "" - - scanner := bufio.NewScanner(source) - for scanner.Scan() { - line := scanner.Text() - if filename := parseLineMarker(line); filename != nil { - inSketch = fileNames.Contains(filename) - if inSketch && removeLineMarkers { - continue - } - } - - if inSketch { - filtered += line + "\n" - } - } - - return filtered -} - -// Parses the given line as a gcc line marker and returns the contained -// filename. -func parseLineMarker(line string) *paths.Path { - // A line marker contains the line number and filename and looks like: - // # 123 /path/to/file.cpp - // It can be followed by zero or more flag number that indicate the - // preprocessor state and can be ignored. - // For exact details on this format, see: - // https://github.com/gcc-mirror/gcc/blob/edd716b6b1caa1a5cb320a8cd7f626f30198e098/gcc/c-family/c-ppoutput.c#L413-L415 - - split := strings.SplitN(line, " ", 3) - if len(split) < 3 || len(split[0]) == 0 || split[0][0] != '#' { - return nil - } - - _, err := strconv.Atoi(split[1]) - if err != nil { - return nil - } - - // If we get here, we found a # followed by a line number, so - // assume this is a line marker and see if the rest of the line - // starts with a string containing the filename - str, rest, ok := utils.ParseCppString(split[2]) - - if ok && (rest == "" || rest[0] == ' ') { - return paths.New(str) - } - return nil -} diff --git a/legacy/builder/test/utils_test.go b/legacy/builder/test/utils_test.go index b6f88555283..71a70be9dd6 100644 --- a/legacy/builder/test/utils_test.go +++ b/legacy/builder/test/utils_test.go @@ -65,41 +65,3 @@ func TestQuoteCppString(t *testing.T) { require.Equal(t, expected, utils.QuoteCppString(input)) } } - -func TestParseCppString(t *testing.T) { - _, _, ok := utils.ParseCppString(`foo`) - require.Equal(t, false, ok) - - _, _, ok = utils.ParseCppString(`"foo`) - require.Equal(t, false, ok) - - str, rest, ok := utils.ParseCppString(`"foo"`) - require.Equal(t, true, ok) - require.Equal(t, `foo`, str) - require.Equal(t, ``, rest) - - str, rest, ok = utils.ParseCppString(`"foo\\bar"`) - require.Equal(t, true, ok) - require.Equal(t, `foo\bar`, str) - require.Equal(t, ``, rest) - - str, rest, ok = utils.ParseCppString(`"foo \"is\" quoted and \\\\bar\"\" escaped\\" and "then" some`) - require.Equal(t, true, ok) - require.Equal(t, `foo "is" quoted and \\bar"" escaped\`, str) - require.Equal(t, ` and "then" some`, rest) - - str, rest, ok = utils.ParseCppString(`" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz{|}~"`) - require.Equal(t, true, ok) - require.Equal(t, ` !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz{|}~`, str) - require.Equal(t, ``, rest) - - str, rest, ok = utils.ParseCppString(`"/home/ççç/"`) - require.Equal(t, true, ok) - require.Equal(t, `/home/ççç/`, str) - require.Equal(t, ``, rest) - - str, rest, ok = utils.ParseCppString(`"/home/ççç/ /$sdsdd\\"`) - require.Equal(t, true, ok) - require.Equal(t, `/home/ççç/ /$sdsdd\`, str) - require.Equal(t, ``, rest) -} diff --git a/legacy/builder/utils/utils.go b/legacy/builder/utils/utils.go index 036fab0dc72..522f046a018 100644 --- a/legacy/builder/utils/utils.go +++ b/legacy/builder/utils/utils.go @@ -26,7 +26,6 @@ import ( "path/filepath" "strings" "unicode" - "unicode/utf8" "github.com/arduino/arduino-cli/i18n" "github.com/arduino/arduino-cli/legacy/builder/gohasissues" @@ -301,50 +300,6 @@ func QuoteCppPath(path *paths.Path) string { return QuoteCppString(path.String()) } -// Parse a C-preprocessor string as emitted by the preprocessor. This -// is a string contained in double quotes, with any backslashes or -// quotes escaped with a backslash. If a valid string was present at the -// start of the given line, returns the unquoted string contents, the -// remainder of the line (everything after the closing "), and true. -// Otherwise, returns the empty string, the entire line and false. -func ParseCppString(line string) (string, string, bool) { - // For details about how these strings are output by gcc, see: - // https://github.com/gcc-mirror/gcc/blob/a588355ab948cf551bc9d2b89f18e5ae5140f52c/libcpp/macro.c#L491-L511 - // Note that the documentation suggests all non-printable - // characters are also escaped, but the implementation does not - // actually do this. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51259 - if len(line) < 1 || line[0] != '"' { - return "", line, false - } - - i := 1 - res := "" - for { - if i >= len(line) { - return "", line, false - } - - c, width := utf8.DecodeRuneInString(line[i:]) - - switch c { - case '\\': - // Backslash, next character is used unmodified - i += width - if i >= len(line) { - return "", line, false - } - res += string(line[i]) - case '"': - // Quote, end of string - return res, line[i+width:], true - default: - res += string(c) - } - - i += width - } -} - // Normalizes an UTF8 byte slice // TODO: use it more often troughout all the project (maybe on logger interface?) func NormalizeUTF8(buf []byte) []byte { From a3ae9d858f78caced776613899b21aac63029bfd Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 25 May 2023 18:22:55 +0200 Subject: [PATCH 11/11] Fixed lint error --- arduino/builder/cpp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arduino/builder/cpp.go b/arduino/builder/cpp.go index 1054388f43f..87610e7ed18 100644 --- a/arduino/builder/cpp.go +++ b/arduino/builder/cpp.go @@ -29,7 +29,7 @@ func QuoteCppString(str string) string { return "\"" + str + "\"" } -// Parse a C-preprocessor string as emitted by the preprocessor. This +// ParseCppString parse a C-preprocessor string as emitted by the preprocessor. This // is a string contained in double quotes, with any backslashes or // quotes escaped with a backslash. If a valid string was present at the // start of the given line, returns the unquoted string contents, the