Skip to content

Commit 7821c24

Browse files
author
Federico Fissore
committed
When sketch has no functions, don't try generating prototypes. Fixes #1
1 parent 2361bc2 commit 7821c24

File tree

6 files changed

+131
-1
lines changed

6 files changed

+131
-1
lines changed

Diff for: src/arduino.cc/builder/prototypes_adder.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ type PrototypesAdder struct{}
4040

4141
func (s *PrototypesAdder) Run(context map[string]interface{}) error {
4242
source := context[constants.CTX_SOURCE].(string)
43+
sourceRows := strings.Split(source, "\n")
4344

4445
if !utils.MapHas(context, constants.CTX_FIRST_FUNCTION_AT_LINE) {
4546
return nil
4647
}
4748

4849
firstFunctionLine := context[constants.CTX_FIRST_FUNCTION_AT_LINE].(int)
49-
firstFunctionChar := len(strings.Join(strings.Split(source, "\n")[:firstFunctionLine-1], "\n")) + 1
50+
if firstFunctionOutsideOfSource(firstFunctionLine, sourceRows) {
51+
return nil
52+
}
53+
54+
firstFunctionChar := len(strings.Join(sourceRows[:firstFunctionLine-1], "\n")) + 1
5055
if firstFunctionLine > 1 {
5156
firstFunctionLine -= context[constants.CTX_LINE_OFFSET].(int)
5257
}
@@ -82,3 +87,7 @@ func composeIncludeArduinoSection() string {
8287

8388
return str
8489
}
90+
91+
func firstFunctionOutsideOfSource(firstFunctionLine int, sourceRows []string) bool {
92+
return firstFunctionLine > len(sourceRows)-1
93+
}

Diff for: src/arduino.cc/builder/test/builder_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ package test
3232
import (
3333
"arduino.cc/builder"
3434
"arduino.cc/builder/constants"
35+
"github.com/stretchr/testify/require"
3536
"os"
3637
"path/filepath"
3738
"testing"
@@ -243,3 +244,23 @@ func TestBuilderBridgeRedBearLab(t *testing.T) {
243244
_, err = os.Stat(filepath.Join(buildPath, constants.FOLDER_LIBRARIES, "Bridge", "Mailbox.cpp.o"))
244245
NoError(t, err)
245246
}
247+
248+
func TestBuilderSketchNoFunctions(t *testing.T) {
249+
DownloadCoresAndToolsAndLibraries(t)
250+
251+
context := make(map[string]interface{})
252+
253+
buildPath := SetupBuildPath(t, context)
254+
defer os.RemoveAll(buildPath)
255+
256+
context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware", "downloaded_board_manager_stuff"}
257+
context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools", "downloaded_board_manager_stuff"}
258+
context[constants.CTX_FQBN] = "RedBearLab:avr:blend"
259+
context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_no_functions", "main.ino")
260+
context[constants.CTX_LIBRARIES_FOLDERS] = []string{"libraries", "downloaded_libraries"}
261+
context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600"
262+
263+
command := builder.Builder{}
264+
err := command.Run(context)
265+
require.Error(t, err)
266+
}

Diff for: src/arduino.cc/builder/test/prototypes_adder_test.go

+80
Original file line numberDiff line numberDiff line change
@@ -435,3 +435,83 @@ func TestPrototypesAdderSketchWithConfig(t *testing.T) {
435435

436436
require.Equal(t, preprocessed, strings.Replace(context[constants.CTX_SOURCE].(string), "\r\n", "\n", -1))
437437
}
438+
439+
func TestPrototypesAdderSketchNoFunctionsTwoFiles(t *testing.T) {
440+
DownloadCoresAndToolsAndLibraries(t)
441+
442+
context := make(map[string]interface{})
443+
444+
buildPath := SetupBuildPath(t, context)
445+
defer os.RemoveAll(buildPath)
446+
447+
context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}
448+
context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"}
449+
context[constants.CTX_FQBN] = "arduino:avr:leonardo"
450+
context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_no_functions_two_files", "main.ino")
451+
context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600"
452+
context[constants.CTX_LIBRARIES_FOLDERS] = []string{"libraries", "downloaded_libraries"}
453+
context[constants.CTX_VERBOSE] = true
454+
455+
commands := []types.Command{
456+
&builder.SetupHumanLoggerIfMissing{},
457+
458+
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
459+
460+
&builder.ContainerMergeCopySketchFiles{},
461+
462+
&builder.ContainerFindIncludes{},
463+
464+
&builder.PrintUsedLibrariesIfVerbose{},
465+
&builder.WarnAboutArchIncompatibleLibraries{},
466+
467+
&builder.ContainerAddPrototypes{},
468+
}
469+
470+
for _, command := range commands {
471+
err := command.Run(context)
472+
NoError(t, err)
473+
}
474+
475+
require.Nil(t, context[constants.CTX_INCLUDE_SECTION])
476+
require.Nil(t, context[constants.CTX_PROTOTYPE_SECTION])
477+
}
478+
479+
func TestPrototypesAdderSketchNoFunctions(t *testing.T) {
480+
DownloadCoresAndToolsAndLibraries(t)
481+
482+
context := make(map[string]interface{})
483+
484+
buildPath := SetupBuildPath(t, context)
485+
defer os.RemoveAll(buildPath)
486+
487+
context[constants.CTX_HARDWARE_FOLDERS] = []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"}
488+
context[constants.CTX_TOOLS_FOLDERS] = []string{"downloaded_tools"}
489+
context[constants.CTX_FQBN] = "arduino:avr:leonardo"
490+
context[constants.CTX_SKETCH_LOCATION] = filepath.Join("sketch_no_functions", "main.ino")
491+
context[constants.CTX_BUILD_PROPERTIES_RUNTIME_IDE_VERSION] = "10600"
492+
context[constants.CTX_LIBRARIES_FOLDERS] = []string{"libraries", "downloaded_libraries"}
493+
context[constants.CTX_VERBOSE] = true
494+
495+
commands := []types.Command{
496+
&builder.SetupHumanLoggerIfMissing{},
497+
498+
&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},
499+
500+
&builder.ContainerMergeCopySketchFiles{},
501+
502+
&builder.ContainerFindIncludes{},
503+
504+
&builder.PrintUsedLibrariesIfVerbose{},
505+
&builder.WarnAboutArchIncompatibleLibraries{},
506+
507+
&builder.ContainerAddPrototypes{},
508+
}
509+
510+
for _, command := range commands {
511+
err := command.Run(context)
512+
NoError(t, err)
513+
}
514+
515+
require.Nil(t, context[constants.CTX_INCLUDE_SECTION])
516+
require.Nil(t, context[constants.CTX_PROTOTYPE_SECTION])
517+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
3+
Test sketch
4+
5+
6+
7+
*/
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
3+
Test sketch
4+
5+
6+
7+
*/
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include <SPI.h>
2+
3+
int a = 0;
4+
int b = 0;

0 commit comments

Comments
 (0)