Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/arduino.cc/builder/ctags/ctags_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func removeDuplicate(tags []*types.CTag) {
definedPrototypes := make(map[string]bool)

for _, tag := range tags {
if !definedPrototypes[tag.Prototype] {
if !definedPrototypes[tag.Prototype] && tag.SkipMe == false {
definedPrototypes[tag.Prototype] = true
} else {
tag.SkipMe = true
Expand Down
57 changes: 57 additions & 0 deletions src/arduino.cc/builder/ctags_target_file_saver.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
"arduino.cc/builder/types"
"arduino.cc/builder/utils"
"path/filepath"
"strconv"
"strings"
)

type CTagsTargetFileSaver struct {
Expand All @@ -50,6 +52,12 @@ func (s *CTagsTargetFileSaver) Run(ctx *types.Context) error {
return i18n.WrapError(err)
}

// drop every line which is not part of user sketch or a define/ifdef/endif/etc
var searchSlice []string
searchSlice = append(searchSlice, filepath.Dir(ctx.SketchLocation))
searchSlice = append(searchSlice, filepath.Dir(ctx.BuildPath))
source = saveLinesContainingDirectivesAndSketch(source, searchSlice)

ctagsTargetFilePath := filepath.Join(preprocPath, s.TargetFileName)
err = utils.WriteFile(ctagsTargetFilePath, source)
if err != nil {
Expand All @@ -60,3 +68,52 @@ func (s *CTagsTargetFileSaver) Run(ctx *types.Context) error {

return nil
}

func saveLinesContainingDirectivesAndSketch(src string, tofind []string) string {
lines := strings.Split(src, "\n")

saveLine := false
minimizedString := ""

for _, line := range lines {
if saveLine || startsWithHashtag(line) {
minimizedString += line + "\n"
}
if containsAny(line, tofind) && isLineMarker(line) {
saveLine = true
}
if saveLine && !containsAny(line, tofind) && isLineMarker(line) {
saveLine = false
}
}
return minimizedString
}

func containsAny(src string, tofind []string) bool {
for _, str := range tofind {
if strings.Contains(src, str) {
return true
}
}
return false
}

func startsWithHashtag(src string) bool {
trimmedStr := strings.TrimSpace(src)
if len(trimmedStr) > 0 && trimmedStr[0] == '#' {
return true
}
return false
}

func isLineMarker(src string) bool {
trimmedStr := strings.TrimSpace(src)
splittedStr := strings.Split(trimmedStr, " ")
if len(splittedStr) > 2 && splittedStr[0][0] == '#' {
_, err := strconv.Atoi(splittedStr[1])
if err == nil {
return true
}
}
return false
}
52 changes: 52 additions & 0 deletions src/arduino.cc/builder/test/ctags_to_prototypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@
package test

import (
"arduino.cc/builder"
"arduino.cc/builder/constants"
"arduino.cc/builder/ctags"
"arduino.cc/builder/types"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
Expand Down Expand Up @@ -459,3 +462,52 @@ func TestCTagsToPrototypesFunctionPointers(t *testing.T) {

require.Equal(t, 2, ctx.PrototypesLineWhereToInsert)
}

func TestCTagsRunnerSketchWithClassFunction(t *testing.T) {
DownloadCoresAndToolsAndLibraries(t)

sketchLocation := Abs(t, filepath.Join("sketch_with_class", "sketch_class_function.ino"))

ctx := &types.Context{
HardwareFolders: []string{filepath.Join("..", "hardware"), "hardware", "downloaded_hardware"},
ToolsFolders: []string{"downloaded_tools"},
BuiltInLibrariesFolders: []string{"downloaded_libraries"},
OtherLibrariesFolders: []string{"libraries"},
SketchLocation: sketchLocation,
FQBN: "arduino:avr:leonardo",
ArduinoAPIVersion: "10600",
Verbose: true,
}

buildPath := SetupBuildPath(t, ctx)
defer os.RemoveAll(buildPath)

commands := []types.Command{

&builder.ContainerSetupHardwareToolsLibsSketchAndProps{},

&builder.ContainerMergeCopySketchFiles{},

&builder.ContainerFindIncludes{},

&builder.PrintUsedLibrariesIfVerbose{},
&builder.WarnAboutArchIncompatibleLibraries{},
&builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: constants.FILE_CTAGS_TARGET},
&ctags.CTagsRunner{},
&ctags.CTagsParser{},
&CollectCtagsFromPreprocSource{},
&ctags.CTagsToPrototypes{},
}

for _, command := range commands {
err := command.Run(ctx)
NoError(t, err)
}

prototypes := ctx.Prototypes

require.Equal(t, 3, len(prototypes))
require.Equal(t, "void setup();", prototypes[0].Prototype)
require.Equal(t, "void loop();", prototypes[1].Prototype)
require.Equal(t, "void asdf();", prototypes[2].Prototype)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class test {
void asdf() {}
};
void setup() {
asdf();
}
void loop() {}
void asdf() {}