diff --git a/src/arduino.cc/builder/container_add_prototypes.go b/src/arduino.cc/builder/container_add_prototypes.go index 378a0689..b69da297 100644 --- a/src/arduino.cc/builder/container_add_prototypes.go +++ b/src/arduino.cc/builder/container_add_prototypes.go @@ -33,7 +33,6 @@ import ( "path/filepath" "arduino.cc/builder/constants" - "arduino.cc/builder/ctags" "arduino.cc/builder/i18n" "arduino.cc/builder/types" ) @@ -47,9 +46,7 @@ func (s *ContainerAddPrototypes) Run(ctx *types.Context) error { &ReadFileAndStoreInContext{Target: &ctx.SourceGccMinusE}, &FilterSketchSource{Source: &ctx.SourceGccMinusE}, &CTagsTargetFileSaver{Source: &ctx.SourceGccMinusE, TargetFileName: constants.FILE_CTAGS_TARGET_FOR_GCC_MINUS_E}, - &ctags.CTagsRunner{}, - &ctags.CTagsParser{}, - &ctags.CTagsToPrototypes{}, + &CTagsRunner{}, &PrototypesAdder{}, &SketchSaver{}, } diff --git a/src/arduino.cc/builder/ctags/ctags_parser.go b/src/arduino.cc/builder/ctags/ctags_parser.go index 349fd32d..c630400d 100644 --- a/src/arduino.cc/builder/ctags/ctags_parser.go +++ b/src/arduino.cc/builder/ctags/ctags_parser.go @@ -30,14 +30,12 @@ package ctags import ( - "arduino.cc/builder/constants" - "arduino.cc/builder/types" "bufio" "os" - "reflect" - "runtime" "strconv" "strings" + + "arduino.cc/builder/types" ) const KIND_PROTOTYPE = "prototype" @@ -54,32 +52,30 @@ var KNOWN_TAG_KINDS = map[string]bool{ "function": true, } -type CTagsParser struct{} - -func (s *CTagsParser) Run(ctx *types.Context) error { - rows := strings.Split(ctx.CTagsOutput, "\n") +type CTagsParser struct { + tags []*types.CTag +} +func (p *CTagsParser) Parse(ctagsOutput string) []*types.CTag { + rows := strings.Split(ctagsOutput, "\n") rows = removeEmpty(rows) - var tags []*types.CTag for _, row := range rows { - tags = append(tags, parseTag(row)) + p.tags = append(p.tags, parseTag(row)) } - skipTagsWhere(tags, tagIsUnknown, ctx) - skipTagsWhere(tags, tagIsUnhandled, ctx) - addPrototypes(tags) - removeDefinedProtypes(tags, ctx) - removeDuplicate(tags) - skipTagsWhere(tags, prototypeAndCodeDontMatch, ctx) - - ctx.CTagsOfPreprocessedSource = tags + p.skipTagsWhere(tagIsUnknown) + p.skipTagsWhere(tagIsUnhandled) + p.addPrototypes() + p.removeDefinedProtypes() + p.skipDuplicates() + p.skipTagsWhere(prototypeAndCodeDontMatch) - return nil + return p.tags } -func addPrototypes(tags []*types.CTag) { - for _, tag := range tags { +func (p *CTagsParser) addPrototypes() { + for _, tag := range p.tags { if !tag.SkipMe { addPrototype(tag) } @@ -108,28 +104,28 @@ func addPrototype(tag *types.CTag) { tag.PrototypeModifiers = strings.TrimSpace(tag.PrototypeModifiers) } -func removeDefinedProtypes(tags []*types.CTag, ctx *types.Context) { +func (p *CTagsParser) removeDefinedProtypes() { definedPrototypes := make(map[string]bool) - for _, tag := range tags { + for _, tag := range p.tags { if tag.Kind == KIND_PROTOTYPE { definedPrototypes[tag.Prototype] = true } } - for _, tag := range tags { + for _, tag := range p.tags { if definedPrototypes[tag.Prototype] { - if ctx.DebugLevel >= 10 { - ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_SKIPPING_TAG_ALREADY_DEFINED, tag.FunctionName) - } + //if ctx.DebugLevel >= 10 { + // ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_SKIPPING_TAG_ALREADY_DEFINED, tag.FunctionName) + //} tag.SkipMe = true } } } -func removeDuplicate(tags []*types.CTag) { +func (p *CTagsParser) skipDuplicates() { definedPrototypes := make(map[string]bool) - for _, tag := range tags { + for _, tag := range p.tags { if !definedPrototypes[tag.Prototype] && tag.SkipMe == false { definedPrototypes[tag.Prototype] = true } else { @@ -140,13 +136,13 @@ func removeDuplicate(tags []*types.CTag) { type skipFuncType func(tag *types.CTag) bool -func skipTagsWhere(tags []*types.CTag, skipFunc skipFuncType, ctx *types.Context) { - for _, tag := range tags { +func (p *CTagsParser) skipTagsWhere(skipFunc skipFuncType) { + for _, tag := range p.tags { if !tag.SkipMe { skip := skipFunc(tag) - if skip && ctx.DebugLevel >= 10 { - ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_SKIPPING_TAG_WITH_REASON, tag.FunctionName, runtime.FuncForPC(reflect.ValueOf(skipFunc).Pointer()).Name()) - } + //if skip && p.debugLevel >= 10 { + // ctx.GetLogger().Fprintln(os.Stdout, constants.LOG_LEVEL_DEBUG, constants.MSG_SKIPPING_TAG_WITH_REASON, tag.FunctionName, runtime.FuncForPC(reflect.ValueOf(skipFunc).Pointer()).Name()) + //} tag.SkipMe = skip } } diff --git a/src/arduino.cc/builder/test/ctags_parser_test.go b/src/arduino.cc/builder/ctags/ctags_parser_test.go similarity index 73% rename from src/arduino.cc/builder/test/ctags_parser_test.go rename to src/arduino.cc/builder/ctags/ctags_parser_test.go index 45f246af..d6390562 100644 --- a/src/arduino.cc/builder/test/ctags_parser_test.go +++ b/src/arduino.cc/builder/ctags/ctags_parser_test.go @@ -27,29 +27,28 @@ * Copyright 2015 Arduino LLC (http://www.arduino.cc/) */ -package test +package ctags import ( - "arduino.cc/builder/ctags" - "arduino.cc/builder/types" - "github.com/stretchr/testify/require" "io/ioutil" "path/filepath" "testing" -) -func TestCTagsParserShouldListPrototypes(t *testing.T) { - ctx := &types.Context{} + "arduino.cc/builder/types" - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldListPrototypes.txt")) - NoError(t, err) + "github.com/stretchr/testify/require" +) - ctx.CTagsOutput = string(bytes) +func produceTags(t *testing.T, filename string) []*types.CTag { + bytes, err := ioutil.ReadFile(filepath.Join("test_data", filename)) + require.NoError(t, err) - ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(ctx) + parser := CTagsParser{} + return parser.Parse(string(bytes)) +} - tags := ctx.CTagsOfPreprocessedSource +func TestCTagsParserShouldListPrototypes(t *testing.T) { + tags := produceTags(t, "TestCTagsParserShouldListPrototypes.txt") require.Equal(t, 8, len(tags)) idx := 0 @@ -87,17 +86,7 @@ func TestCTagsParserShouldListPrototypes(t *testing.T) { } func TestCTagsParserShouldListTemplates(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldListTemplates.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(ctx) - - tags := ctx.CTagsOfPreprocessedSource + tags := produceTags(t, "TestCTagsParserShouldListTemplates.txt") require.Equal(t, 3, len(tags)) idx := 0 @@ -115,17 +104,7 @@ func TestCTagsParserShouldListTemplates(t *testing.T) { } func TestCTagsParserShouldListTemplates2(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldListTemplates2.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(ctx) - - tags := ctx.CTagsOfPreprocessedSource + tags := produceTags(t, "TestCTagsParserShouldListTemplates2.txt") require.Equal(t, 4, len(tags)) idx := 0 @@ -145,17 +124,7 @@ func TestCTagsParserShouldListTemplates2(t *testing.T) { } func TestCTagsParserShouldDealWithClasses(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealWithClasses.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(ctx) - - tags := ctx.CTagsOfPreprocessedSource + tags := produceTags(t, "TestCTagsParserShouldDealWithClasses.txt") require.Equal(t, 2, len(tags)) idx := 0 @@ -167,17 +136,7 @@ func TestCTagsParserShouldDealWithClasses(t *testing.T) { } func TestCTagsParserShouldDealWithStructs(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealWithStructs.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(ctx) - - tags := ctx.CTagsOfPreprocessedSource + tags := produceTags(t, "TestCTagsParserShouldDealWithStructs.txt") require.Equal(t, 5, len(tags)) idx := 0 @@ -199,17 +158,7 @@ func TestCTagsParserShouldDealWithStructs(t *testing.T) { } func TestCTagsParserShouldDealWithMacros(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealWithMacros.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(ctx) - - tags := ctx.CTagsOfPreprocessedSource + tags := produceTags(t, "TestCTagsParserShouldDealWithMacros.txt") require.Equal(t, 8, len(tags)) idx := 0 @@ -239,17 +188,7 @@ func TestCTagsParserShouldDealWithMacros(t *testing.T) { } func TestCTagsParserShouldDealFunctionWithDifferentSignatures(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealFunctionWithDifferentSignatures.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(ctx) - - tags := ctx.CTagsOfPreprocessedSource + tags := produceTags(t, "TestCTagsParserShouldDealFunctionWithDifferentSignatures.txt") require.Equal(t, 3, len(tags)) idx := 0 @@ -264,17 +203,7 @@ func TestCTagsParserShouldDealFunctionWithDifferentSignatures(t *testing.T) { } func TestCTagsParserClassMembersAreFilteredOut(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserClassMembersAreFilteredOut.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(ctx) - - tags := ctx.CTagsOfPreprocessedSource + tags := produceTags(t, "TestCTagsParserClassMembersAreFilteredOut.txt") require.Equal(t, 5, len(tags)) idx := 0 @@ -298,17 +227,7 @@ func TestCTagsParserClassMembersAreFilteredOut(t *testing.T) { } func TestCTagsParserStructWithFunctions(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserStructWithFunctions.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(ctx) - - tags := ctx.CTagsOfPreprocessedSource + tags := produceTags(t, "TestCTagsParserStructWithFunctions.txt") require.Equal(t, 8, len(tags)) idx := 0 @@ -340,17 +259,7 @@ func TestCTagsParserStructWithFunctions(t *testing.T) { } func TestCTagsParserDefaultArguments(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserDefaultArguments.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(ctx) - - tags := ctx.CTagsOfPreprocessedSource + tags := produceTags(t, "TestCTagsParserDefaultArguments.txt") require.Equal(t, 3, len(tags)) idx := 0 @@ -366,17 +275,7 @@ func TestCTagsParserDefaultArguments(t *testing.T) { } func TestCTagsParserNamespace(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserNamespace.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(ctx) - - tags := ctx.CTagsOfPreprocessedSource + tags := produceTags(t, "TestCTagsParserNamespace.txt") require.Equal(t, 3, len(tags)) idx := 0 @@ -392,17 +291,7 @@ func TestCTagsParserNamespace(t *testing.T) { } func TestCTagsParserStatic(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserStatic.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(ctx) - - tags := ctx.CTagsOfPreprocessedSource + tags := produceTags(t, "TestCTagsParserStatic.txt") require.Equal(t, 3, len(tags)) idx := 0 @@ -417,17 +306,7 @@ func TestCTagsParserStatic(t *testing.T) { } func TestCTagsParserFunctionPointer(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserFunctionPointer.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(ctx) - - tags := ctx.CTagsOfPreprocessedSource + tags := produceTags(t, "TestCTagsParserFunctionPointer.txt") require.Equal(t, 4, len(tags)) idx := 0 @@ -445,17 +324,7 @@ func TestCTagsParserFunctionPointer(t *testing.T) { } func TestCTagsParserFunctionPointers(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserFunctionPointers.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - ctagsParser := ctags.CTagsParser{} - ctagsParser.Run(ctx) - - tags := ctx.CTagsOfPreprocessedSource + tags := produceTags(t, "TestCTagsParserFunctionPointers.txt") require.Equal(t, 5, len(tags)) idx := 0 diff --git a/src/arduino.cc/builder/ctags/ctags_to_prototypes.go b/src/arduino.cc/builder/ctags/ctags_to_prototypes.go index 954cb424..459174d8 100644 --- a/src/arduino.cc/builder/ctags/ctags_to_prototypes.go +++ b/src/arduino.cc/builder/ctags/ctags_to_prototypes.go @@ -30,27 +30,18 @@ package ctags import ( - "arduino.cc/builder/types" "strings" -) - -type CTagsToPrototypes struct{} - -func (s *CTagsToPrototypes) Run(ctx *types.Context) error { - tags := ctx.CTagsOfPreprocessedSource - lineWhereToInsertPrototypes := findLineWhereToInsertPrototypes(tags) - if lineWhereToInsertPrototypes != -1 { - ctx.PrototypesLineWhereToInsert = lineWhereToInsertPrototypes - } + "arduino.cc/builder/types" +) - ctx.Prototypes = toPrototypes(tags) - return nil +func (p *CTagsParser) GeneratePrototypes() ([]*types.Prototype, int) { + return p.toPrototypes(), p.findLineWhereToInsertPrototypes() } -func findLineWhereToInsertPrototypes(tags []*types.CTag) int { - firstFunctionLine := firstFunctionAtLine(tags) - firstFunctionPointerAsArgument := firstFunctionPointerUsedAsArgument(tags) +func (p *CTagsParser) findLineWhereToInsertPrototypes() int { + firstFunctionLine := p.firstFunctionAtLine() + firstFunctionPointerAsArgument := p.firstFunctionPointerUsedAsArgument() if firstFunctionLine != -1 && firstFunctionPointerAsArgument != -1 { if firstFunctionLine < firstFunctionPointerAsArgument { return firstFunctionLine @@ -64,9 +55,9 @@ func findLineWhereToInsertPrototypes(tags []*types.CTag) int { } } -func firstFunctionPointerUsedAsArgument(tags []*types.CTag) int { - functionNames := collectFunctionNames(tags) - for _, tag := range tags { +func (p *CTagsParser) firstFunctionPointerUsedAsArgument() int { + functionNames := p.collectFunctionNames() + for _, tag := range p.tags { if functionNameUsedAsFunctionPointerIn(tag, functionNames) { return tag.Line } @@ -83,9 +74,9 @@ func functionNameUsedAsFunctionPointerIn(tag *types.CTag, functionNames []string return false } -func collectFunctionNames(tags []*types.CTag) []string { +func (p *CTagsParser) collectFunctionNames() []string { names := []string{} - for _, tag := range tags { + for _, tag := range p.tags { if tag.Kind == KIND_FUNCTION { names = append(names, tag.FunctionName) } @@ -93,8 +84,8 @@ func collectFunctionNames(tags []*types.CTag) []string { return names } -func firstFunctionAtLine(tags []*types.CTag) int { - for _, tag := range tags { +func (p *CTagsParser) firstFunctionAtLine() int { + for _, tag := range p.tags { if !tagIsUnknown(tag) && isHandled(tag) && tag.Kind == KIND_FUNCTION { return tag.Line } @@ -102,9 +93,9 @@ func firstFunctionAtLine(tags []*types.CTag) int { return -1 } -func toPrototypes(tags []*types.CTag) []*types.Prototype { +func (p *CTagsParser) toPrototypes() []*types.Prototype { prototypes := []*types.Prototype{} - for _, tag := range tags { + for _, tag := range p.tags { if strings.TrimSpace(tag.Prototype) == "" { continue } diff --git a/src/arduino.cc/builder/ctags/ctags_to_prototypes_test.go b/src/arduino.cc/builder/ctags/ctags_to_prototypes_test.go new file mode 100644 index 00000000..27db5820 --- /dev/null +++ b/src/arduino.cc/builder/ctags/ctags_to_prototypes_test.go @@ -0,0 +1,219 @@ +/* + * This file is part of Arduino Builder. + * + * Arduino Builder is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + * + * Copyright 2015 Arduino LLC (http://www.arduino.cc/) + */ + +package ctags + +import ( + "io/ioutil" + "path/filepath" + "testing" + + "arduino.cc/builder/types" + "github.com/stretchr/testify/require" +) + +func producePrototypes(t *testing.T, filename string) ([]*types.Prototype, int) { + bytes, err := ioutil.ReadFile(filepath.Join("test_data", filename)) + require.NoError(t, err) + + parser := &CTagsParser{} + parser.Parse(string(bytes)) + return parser.GeneratePrototypes() +} + +func TestCTagsToPrototypesShouldListPrototypes(t *testing.T) { + prototypes, line := producePrototypes(t, "TestCTagsParserShouldListPrototypes.txt") + require.Equal(t, 5, len(prototypes)) + require.Equal(t, "void setup();", prototypes[0].Prototype) + require.Equal(t, "/tmp/sketch7210316334309249705.cpp", prototypes[0].File) + require.Equal(t, "void loop();", prototypes[1].Prototype) + require.Equal(t, "void digitalCommand(YunClient client);", prototypes[2].Prototype) + require.Equal(t, "void analogCommand(YunClient client);", prototypes[3].Prototype) + require.Equal(t, "void modeCommand(YunClient client);", prototypes[4].Prototype) + + require.Equal(t, 33, line) +} + +func TestCTagsToPrototypesShouldListTemplates(t *testing.T) { + prototypes, line := producePrototypes(t, "TestCTagsParserShouldListTemplates.txt") + + require.Equal(t, 3, len(prototypes)) + require.Equal(t, "template T minimum (T a, T b);", prototypes[0].Prototype) + require.Equal(t, "/tmp/sketch8398023134925534708.cpp", prototypes[0].File) + require.Equal(t, "void setup();", prototypes[1].Prototype) + require.Equal(t, "void loop();", prototypes[2].Prototype) + + require.Equal(t, 2, line) +} + +func TestCTagsToPrototypesShouldListTemplates2(t *testing.T) { + prototypes, line := producePrototypes(t, "TestCTagsParserShouldListTemplates2.txt") + + require.Equal(t, 4, len(prototypes)) + require.Equal(t, "void setup();", prototypes[0].Prototype) + require.Equal(t, "/tmp/sketch463160524247569568.cpp", prototypes[0].File) + require.Equal(t, "void loop();", prototypes[1].Prototype) + require.Equal(t, "template int SRAM_writeAnything(int ee, const T& value);", prototypes[2].Prototype) + require.Equal(t, "template int SRAM_readAnything(int ee, T& value);", prototypes[3].Prototype) + + require.Equal(t, 1, line) +} + +func TestCTagsToPrototypesShouldDealWithClasses(t *testing.T) { + prototypes, line := producePrototypes(t, "TestCTagsParserShouldDealWithClasses.txt") + + require.Equal(t, 0, len(prototypes)) + + require.Equal(t, 8, line) +} + +func TestCTagsToPrototypesShouldDealWithStructs(t *testing.T) { + prototypes, line := producePrototypes(t, "TestCTagsParserShouldDealWithStructs.txt") + + require.Equal(t, 3, len(prototypes)) + require.Equal(t, "void setup();", prototypes[0].Prototype) + require.Equal(t, "/tmp/sketch8930345717354294915.cpp", prototypes[0].File) + require.Equal(t, "void loop();", prototypes[1].Prototype) + require.Equal(t, "void dostuff(A_NEW_TYPE * bar);", prototypes[2].Prototype) + + require.Equal(t, 9, line) +} + +func TestCTagsToPrototypesShouldDealWithMacros(t *testing.T) { + prototypes, line := producePrototypes(t, "TestCTagsParserShouldDealWithMacros.txt") + + require.Equal(t, 5, len(prototypes)) + require.Equal(t, "void setup();", prototypes[0].Prototype) + require.Equal(t, "/tmp/sketch5976699731718729500.cpp", prototypes[0].File) + require.Equal(t, "void loop();", prototypes[1].Prototype) + require.Equal(t, "void debug();", prototypes[2].Prototype) + require.Equal(t, "void disabledIsDefined();", prototypes[3].Prototype) + require.Equal(t, "int useMyType(MyType type);", prototypes[4].Prototype) + + require.Equal(t, 18, line) +} + +func TestCTagsToPrototypesShouldDealFunctionWithDifferentSignatures(t *testing.T) { + prototypes, line := producePrototypes(t, "TestCTagsParserShouldDealFunctionWithDifferentSignatures.txt") + + require.Equal(t, 1, len(prototypes)) + require.Equal(t, "boolean getBytes( byte addr, int amount );", prototypes[0].Prototype) + require.Equal(t, "/tmp/test260613593/preproc/ctags_target.cpp", prototypes[0].File) + + require.Equal(t, 5031, line) +} + +func TestCTagsToPrototypesClassMembersAreFilteredOut(t *testing.T) { + prototypes, line := producePrototypes(t, "TestCTagsParserClassMembersAreFilteredOut.txt") + + require.Equal(t, 2, len(prototypes)) + require.Equal(t, "void setup();", prototypes[0].Prototype) + require.Equal(t, "/tmp/test834438754/preproc/ctags_target.cpp", prototypes[0].File) + require.Equal(t, "void loop();", prototypes[1].Prototype) + + require.Equal(t, 14, line) +} + +func TestCTagsToPrototypesStructWithFunctions(t *testing.T) { + prototypes, line := producePrototypes(t, "TestCTagsParserStructWithFunctions.txt") + + require.Equal(t, 2, len(prototypes)) + require.Equal(t, "void setup();", prototypes[0].Prototype) + require.Equal(t, "/tmp/build7315640391316178285.tmp/preproc/ctags_target.cpp", prototypes[0].File) + require.Equal(t, "void loop();", prototypes[1].Prototype) + + require.Equal(t, 16, line) +} + +func TestCTagsToPrototypesDefaultArguments(t *testing.T) { + prototypes, line := producePrototypes(t, "TestCTagsParserDefaultArguments.txt") + + require.Equal(t, 3, len(prototypes)) + require.Equal(t, "void test(int x = 1);", prototypes[0].Prototype) + require.Equal(t, "void setup();", prototypes[1].Prototype) + require.Equal(t, "/tmp/test179252494/preproc/ctags_target.cpp", prototypes[1].File) + require.Equal(t, "void loop();", prototypes[2].Prototype) + + require.Equal(t, 2, line) +} + +func TestCTagsToPrototypesNamespace(t *testing.T) { + prototypes, line := producePrototypes(t, "TestCTagsParserNamespace.txt") + + require.Equal(t, 2, len(prototypes)) + require.Equal(t, "void setup();", prototypes[0].Prototype) + require.Equal(t, "/tmp/test030883150/preproc/ctags_target.cpp", prototypes[0].File) + require.Equal(t, "void loop();", prototypes[1].Prototype) + + require.Equal(t, 8, line) +} + +func TestCTagsToPrototypesStatic(t *testing.T) { + prototypes, line := producePrototypes(t, "TestCTagsParserStatic.txt") + + require.Equal(t, 3, len(prototypes)) + require.Equal(t, "void setup();", prototypes[0].Prototype) + require.Equal(t, "/tmp/test542833488/preproc/ctags_target.cpp", prototypes[0].File) + require.Equal(t, "void loop();", prototypes[1].Prototype) + require.Equal(t, "void doStuff();", prototypes[2].Prototype) + require.Equal(t, "static", prototypes[2].Modifiers) + + require.Equal(t, 2, line) +} + +func TestCTagsToPrototypesFunctionPointer(t *testing.T) { + prototypes, line := producePrototypes(t, "TestCTagsParserFunctionPointer.txt") + + require.Equal(t, 3, len(prototypes)) + require.Equal(t, "void t1Callback();", prototypes[0].Prototype) + require.Equal(t, "/tmp/test547238273/preproc/ctags_target.cpp", prototypes[0].File) + require.Equal(t, "void setup();", prototypes[1].Prototype) + require.Equal(t, "void loop();", prototypes[2].Prototype) + + require.Equal(t, 2, line) +} + +func TestCTagsToPrototypesFunctionPointers(t *testing.T) { + prototypes, line := producePrototypes(t, "TestCTagsParserFunctionPointers.txt") + require.Equal(t, 2, len(prototypes)) + require.Equal(t, "void setup();", prototypes[0].Prototype) + require.Equal(t, "/tmp/test907446433/preproc/ctags_target.cpp", prototypes[0].File) + require.Equal(t, "void loop();", prototypes[1].Prototype) + + require.Equal(t, 2, line) +} + +func TestCTagsRunnerSketchWithClassFunction(t *testing.T) { + prototypes, _ := producePrototypes(t, "TestCTagsRunnerSketchWithClassFunction.txt") + + 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) +} diff --git a/src/arduino.cc/builder/test/ctags_output/TestCTagsParserClassMembersAreFilteredOut.txt b/src/arduino.cc/builder/ctags/test_data/TestCTagsParserClassMembersAreFilteredOut.txt similarity index 100% rename from src/arduino.cc/builder/test/ctags_output/TestCTagsParserClassMembersAreFilteredOut.txt rename to src/arduino.cc/builder/ctags/test_data/TestCTagsParserClassMembersAreFilteredOut.txt diff --git a/src/arduino.cc/builder/test/ctags_output/TestCTagsParserDefaultArguments.txt b/src/arduino.cc/builder/ctags/test_data/TestCTagsParserDefaultArguments.txt similarity index 100% rename from src/arduino.cc/builder/test/ctags_output/TestCTagsParserDefaultArguments.txt rename to src/arduino.cc/builder/ctags/test_data/TestCTagsParserDefaultArguments.txt diff --git a/src/arduino.cc/builder/test/ctags_output/TestCTagsParserFunctionPointer.txt b/src/arduino.cc/builder/ctags/test_data/TestCTagsParserFunctionPointer.txt similarity index 100% rename from src/arduino.cc/builder/test/ctags_output/TestCTagsParserFunctionPointer.txt rename to src/arduino.cc/builder/ctags/test_data/TestCTagsParserFunctionPointer.txt diff --git a/src/arduino.cc/builder/test/ctags_output/TestCTagsParserFunctionPointers.txt b/src/arduino.cc/builder/ctags/test_data/TestCTagsParserFunctionPointers.txt similarity index 100% rename from src/arduino.cc/builder/test/ctags_output/TestCTagsParserFunctionPointers.txt rename to src/arduino.cc/builder/ctags/test_data/TestCTagsParserFunctionPointers.txt diff --git a/src/arduino.cc/builder/test/ctags_output/TestCTagsParserNamespace.txt b/src/arduino.cc/builder/ctags/test_data/TestCTagsParserNamespace.txt similarity index 100% rename from src/arduino.cc/builder/test/ctags_output/TestCTagsParserNamespace.txt rename to src/arduino.cc/builder/ctags/test_data/TestCTagsParserNamespace.txt diff --git a/src/arduino.cc/builder/test/ctags_output/TestCTagsParserShouldDealFunctionWithDifferentSignatures.txt b/src/arduino.cc/builder/ctags/test_data/TestCTagsParserShouldDealFunctionWithDifferentSignatures.txt similarity index 100% rename from src/arduino.cc/builder/test/ctags_output/TestCTagsParserShouldDealFunctionWithDifferentSignatures.txt rename to src/arduino.cc/builder/ctags/test_data/TestCTagsParserShouldDealFunctionWithDifferentSignatures.txt diff --git a/src/arduino.cc/builder/test/ctags_output/TestCTagsParserShouldDealWithClasses.txt b/src/arduino.cc/builder/ctags/test_data/TestCTagsParserShouldDealWithClasses.txt similarity index 100% rename from src/arduino.cc/builder/test/ctags_output/TestCTagsParserShouldDealWithClasses.txt rename to src/arduino.cc/builder/ctags/test_data/TestCTagsParserShouldDealWithClasses.txt diff --git a/src/arduino.cc/builder/test/ctags_output/TestCTagsParserShouldDealWithMacros.txt b/src/arduino.cc/builder/ctags/test_data/TestCTagsParserShouldDealWithMacros.txt similarity index 100% rename from src/arduino.cc/builder/test/ctags_output/TestCTagsParserShouldDealWithMacros.txt rename to src/arduino.cc/builder/ctags/test_data/TestCTagsParserShouldDealWithMacros.txt diff --git a/src/arduino.cc/builder/test/ctags_output/TestCTagsParserShouldDealWithStructs.txt b/src/arduino.cc/builder/ctags/test_data/TestCTagsParserShouldDealWithStructs.txt similarity index 100% rename from src/arduino.cc/builder/test/ctags_output/TestCTagsParserShouldDealWithStructs.txt rename to src/arduino.cc/builder/ctags/test_data/TestCTagsParserShouldDealWithStructs.txt diff --git a/src/arduino.cc/builder/test/ctags_output/TestCTagsParserShouldListPrototypes.txt b/src/arduino.cc/builder/ctags/test_data/TestCTagsParserShouldListPrototypes.txt similarity index 100% rename from src/arduino.cc/builder/test/ctags_output/TestCTagsParserShouldListPrototypes.txt rename to src/arduino.cc/builder/ctags/test_data/TestCTagsParserShouldListPrototypes.txt diff --git a/src/arduino.cc/builder/test/ctags_output/TestCTagsParserShouldListTemplates.txt b/src/arduino.cc/builder/ctags/test_data/TestCTagsParserShouldListTemplates.txt similarity index 100% rename from src/arduino.cc/builder/test/ctags_output/TestCTagsParserShouldListTemplates.txt rename to src/arduino.cc/builder/ctags/test_data/TestCTagsParserShouldListTemplates.txt diff --git a/src/arduino.cc/builder/test/ctags_output/TestCTagsParserShouldListTemplates2.txt b/src/arduino.cc/builder/ctags/test_data/TestCTagsParserShouldListTemplates2.txt similarity index 100% rename from src/arduino.cc/builder/test/ctags_output/TestCTagsParserShouldListTemplates2.txt rename to src/arduino.cc/builder/ctags/test_data/TestCTagsParserShouldListTemplates2.txt diff --git a/src/arduino.cc/builder/test/ctags_output/TestCTagsParserStatic.txt b/src/arduino.cc/builder/ctags/test_data/TestCTagsParserStatic.txt similarity index 100% rename from src/arduino.cc/builder/test/ctags_output/TestCTagsParserStatic.txt rename to src/arduino.cc/builder/ctags/test_data/TestCTagsParserStatic.txt diff --git a/src/arduino.cc/builder/test/ctags_output/TestCTagsParserStructWithFunctions.txt b/src/arduino.cc/builder/ctags/test_data/TestCTagsParserStructWithFunctions.txt similarity index 100% rename from src/arduino.cc/builder/test/ctags_output/TestCTagsParserStructWithFunctions.txt rename to src/arduino.cc/builder/ctags/test_data/TestCTagsParserStructWithFunctions.txt diff --git a/src/arduino.cc/builder/ctags/test_data/TestCTagsRunnerSketchWithClassFunction.txt b/src/arduino.cc/builder/ctags/test_data/TestCTagsRunnerSketchWithClassFunction.txt new file mode 100644 index 00000000..8d83586a --- /dev/null +++ b/src/arduino.cc/builder/ctags/test_data/TestCTagsRunnerSketchWithClassFunction.txt @@ -0,0 +1,4 @@ +asdf /home/megabug/Workspace/arduino-builder/src/arduino.cc/builder/test/sketch_class_function/sketch_class_function.ino /^ void asdf() {}$/;" kind:function line:2 class:test signature:() returntype:void +setup /home/megabug/Workspace/arduino-builder/src/arduino.cc/builder/test/sketch_class_function/sketch_class_function.ino /^void setup() {$/;" kind:function line:4 signature:() returntype:void +loop /home/megabug/Workspace/arduino-builder/src/arduino.cc/builder/test/sketch_class_function/sketch_class_function.ino /^void loop() {}$/;" kind:function line:7 signature:() returntype:void +asdf /home/megabug/Workspace/arduino-builder/src/arduino.cc/builder/test/sketch_class_function/sketch_class_function.ino /^void asdf() {}$/;" kind:function line:8 signature:() returntype:void diff --git a/src/arduino.cc/builder/ctags/ctags_runner.go b/src/arduino.cc/builder/ctags_runner.go similarity index 90% rename from src/arduino.cc/builder/ctags/ctags_runner.go rename to src/arduino.cc/builder/ctags_runner.go index b58242d7..b869d6f3 100644 --- a/src/arduino.cc/builder/ctags/ctags_runner.go +++ b/src/arduino.cc/builder/ctags_runner.go @@ -27,14 +27,16 @@ * Copyright 2015 Arduino LLC (http://www.arduino.cc/) */ -package ctags +package builder import ( + "fmt" + "arduino.cc/builder/constants" + "arduino.cc/builder/ctags" "arduino.cc/builder/i18n" "arduino.cc/builder/types" "arduino.cc/builder/utils" - "fmt" ) type CTagsRunner struct{} @@ -71,5 +73,13 @@ func (s *CTagsRunner) Run(ctx *types.Context) error { ctx.CTagsOutput = string(sourceBytes) + parser := &ctags.CTagsParser{} + ctx.CTagsOfPreprocessedSource = parser.Parse(ctx.CTagsOutput) + protos, line := parser.GeneratePrototypes() + if line != -1 { + ctx.PrototypesLineWhereToInsert = line + } + ctx.Prototypes = protos + return nil } diff --git a/src/arduino.cc/builder/test/ctags_runner_test.go b/src/arduino.cc/builder/test/ctags_runner_test.go index d7d96ef4..b090df0a 100644 --- a/src/arduino.cc/builder/test/ctags_runner_test.go +++ b/src/arduino.cc/builder/test/ctags_runner_test.go @@ -30,15 +30,15 @@ package test import ( - "arduino.cc/builder" - "arduino.cc/builder/constants" - "arduino.cc/builder/ctags" - "arduino.cc/builder/types" - "github.com/stretchr/testify/require" "os" "path/filepath" "strings" "testing" + + "arduino.cc/builder" + "arduino.cc/builder/constants" + "arduino.cc/builder/types" + "github.com/stretchr/testify/require" ) func TestCTagsRunner(t *testing.T) { @@ -71,7 +71,7 @@ func TestCTagsRunner(t *testing.T) { &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: constants.FILE_CTAGS_TARGET}, - &ctags.CTagsRunner{}, + &builder.CTagsRunner{}, } for _, command := range commands { @@ -121,7 +121,7 @@ func TestCTagsRunnerSketchWithClass(t *testing.T) { &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: constants.FILE_CTAGS_TARGET}, - &ctags.CTagsRunner{}, + &builder.CTagsRunner{}, } for _, command := range commands { @@ -169,7 +169,7 @@ func TestCTagsRunnerSketchWithTypename(t *testing.T) { &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: constants.FILE_CTAGS_TARGET}, - &ctags.CTagsRunner{}, + &builder.CTagsRunner{}, } for _, command := range commands { @@ -216,7 +216,7 @@ func TestCTagsRunnerSketchWithNamespace(t *testing.T) { &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: constants.FILE_CTAGS_TARGET}, - &ctags.CTagsRunner{}, + &builder.CTagsRunner{}, } for _, command := range commands { @@ -262,7 +262,7 @@ func TestCTagsRunnerSketchWithTemplates(t *testing.T) { &builder.PrintUsedLibrariesIfVerbose{}, &builder.WarnAboutArchIncompatibleLibraries{}, &builder.CTagsTargetFileSaver{Source: &ctx.Source, TargetFileName: constants.FILE_CTAGS_TARGET}, - &ctags.CTagsRunner{}, + &builder.CTagsRunner{}, } for _, command := range commands { diff --git a/src/arduino.cc/builder/test/ctags_to_prototypes_test.go b/src/arduino.cc/builder/test/ctags_to_prototypes_test.go deleted file mode 100644 index 1f90395a..00000000 --- a/src/arduino.cc/builder/test/ctags_to_prototypes_test.go +++ /dev/null @@ -1,492 +0,0 @@ -/* - * This file is part of Arduino Builder. - * - * Arduino Builder is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - * - * As a special exception, you may use this file as part of a free software - * library without restriction. Specifically, if other files instantiate - * templates or use macros or inline functions from this file, or you compile - * this file and link it with other files to produce an executable, this - * file does not by itself cause the resulting executable to be covered by - * the GNU General Public License. This exception does not however - * invalidate any other reasons why the executable file might be covered by - * the GNU General Public License. - * - * Copyright 2015 Arduino LLC (http://www.arduino.cc/) - */ - -package test - -import ( - "io/ioutil" - "os" - "path/filepath" - "testing" - - "arduino.cc/builder" - "arduino.cc/builder/constants" - "arduino.cc/builder/ctags" - "arduino.cc/builder/types" - "github.com/stretchr/testify/require" -) - -func TestCTagsToPrototypesShouldListPrototypes(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldListPrototypes.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - commands := []types.Command{ - &ctags.CTagsParser{}, - &ctags.CTagsToPrototypes{}, - } - - for _, command := range commands { - err := command.Run(ctx) - NoError(t, err) - } - - prototypes := ctx.Prototypes - - require.Equal(t, 5, len(prototypes)) - require.Equal(t, "void setup();", prototypes[0].Prototype) - require.Equal(t, "/tmp/sketch7210316334309249705.cpp", prototypes[0].File) - require.Equal(t, "void loop();", prototypes[1].Prototype) - require.Equal(t, "void digitalCommand(YunClient client);", prototypes[2].Prototype) - require.Equal(t, "void analogCommand(YunClient client);", prototypes[3].Prototype) - require.Equal(t, "void modeCommand(YunClient client);", prototypes[4].Prototype) - - require.Equal(t, 33, ctx.PrototypesLineWhereToInsert) -} - -func TestCTagsToPrototypesShouldListTemplates(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldListTemplates.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - commands := []types.Command{ - &ctags.CTagsParser{}, - &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, "template T minimum (T a, T b);", prototypes[0].Prototype) - require.Equal(t, "/tmp/sketch8398023134925534708.cpp", prototypes[0].File) - require.Equal(t, "void setup();", prototypes[1].Prototype) - require.Equal(t, "void loop();", prototypes[2].Prototype) - - require.Equal(t, 2, ctx.PrototypesLineWhereToInsert) -} - -func TestCTagsToPrototypesShouldListTemplates2(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldListTemplates2.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - commands := []types.Command{ - &ctags.CTagsParser{}, - &ctags.CTagsToPrototypes{}, - } - - for _, command := range commands { - err := command.Run(ctx) - NoError(t, err) - } - - prototypes := ctx.Prototypes - - require.Equal(t, 4, len(prototypes)) - require.Equal(t, "void setup();", prototypes[0].Prototype) - require.Equal(t, "/tmp/sketch463160524247569568.cpp", prototypes[0].File) - require.Equal(t, "void loop();", prototypes[1].Prototype) - require.Equal(t, "template int SRAM_writeAnything(int ee, const T& value);", prototypes[2].Prototype) - require.Equal(t, "template int SRAM_readAnything(int ee, T& value);", prototypes[3].Prototype) - - require.Equal(t, 1, ctx.PrototypesLineWhereToInsert) -} - -func TestCTagsToPrototypesShouldDealWithClasses(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealWithClasses.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - commands := []types.Command{ - &ctags.CTagsParser{}, - &ctags.CTagsToPrototypes{}, - } - - for _, command := range commands { - err := command.Run(ctx) - NoError(t, err) - } - - prototypes := ctx.Prototypes - - require.Equal(t, 0, len(prototypes)) - - require.Equal(t, 8, ctx.PrototypesLineWhereToInsert) -} - -func TestCTagsToPrototypesShouldDealWithStructs(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealWithStructs.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - commands := []types.Command{ - &ctags.CTagsParser{}, - &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, "/tmp/sketch8930345717354294915.cpp", prototypes[0].File) - require.Equal(t, "void loop();", prototypes[1].Prototype) - require.Equal(t, "void dostuff(A_NEW_TYPE * bar);", prototypes[2].Prototype) - - require.Equal(t, 9, ctx.PrototypesLineWhereToInsert) -} - -func TestCTagsToPrototypesShouldDealWithMacros(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealWithMacros.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - commands := []types.Command{ - &ctags.CTagsParser{}, - &ctags.CTagsToPrototypes{}, - } - - for _, command := range commands { - err := command.Run(ctx) - NoError(t, err) - } - - prototypes := ctx.Prototypes - - require.Equal(t, 5, len(prototypes)) - require.Equal(t, "void setup();", prototypes[0].Prototype) - require.Equal(t, "/tmp/sketch5976699731718729500.cpp", prototypes[0].File) - require.Equal(t, "void loop();", prototypes[1].Prototype) - require.Equal(t, "void debug();", prototypes[2].Prototype) - require.Equal(t, "void disabledIsDefined();", prototypes[3].Prototype) - require.Equal(t, "int useMyType(MyType type);", prototypes[4].Prototype) - - require.Equal(t, 18, ctx.PrototypesLineWhereToInsert) -} - -func TestCTagsToPrototypesShouldDealFunctionWithDifferentSignatures(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserShouldDealFunctionWithDifferentSignatures.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - commands := []types.Command{ - &ctags.CTagsParser{}, - &ctags.CTagsToPrototypes{}, - } - - for _, command := range commands { - err := command.Run(ctx) - NoError(t, err) - } - - prototypes := ctx.Prototypes - - require.Equal(t, 1, len(prototypes)) - require.Equal(t, "boolean getBytes( byte addr, int amount );", prototypes[0].Prototype) - require.Equal(t, "/tmp/test260613593/preproc/ctags_target.cpp", prototypes[0].File) - - require.Equal(t, 5031, ctx.PrototypesLineWhereToInsert) -} - -func TestCTagsToPrototypesClassMembersAreFilteredOut(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserClassMembersAreFilteredOut.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - commands := []types.Command{ - &ctags.CTagsParser{}, - &ctags.CTagsToPrototypes{}, - } - - for _, command := range commands { - err := command.Run(ctx) - NoError(t, err) - } - - prototypes := ctx.Prototypes - - require.Equal(t, 2, len(prototypes)) - require.Equal(t, "void setup();", prototypes[0].Prototype) - require.Equal(t, "/tmp/test834438754/preproc/ctags_target.cpp", prototypes[0].File) - require.Equal(t, "void loop();", prototypes[1].Prototype) - - require.Equal(t, 14, ctx.PrototypesLineWhereToInsert) -} - -func TestCTagsToPrototypesStructWithFunctions(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserStructWithFunctions.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - commands := []types.Command{ - &ctags.CTagsParser{}, - &ctags.CTagsToPrototypes{}, - } - - for _, command := range commands { - err := command.Run(ctx) - NoError(t, err) - } - - prototypes := ctx.Prototypes - - require.Equal(t, 2, len(prototypes)) - require.Equal(t, "void setup();", prototypes[0].Prototype) - require.Equal(t, "/tmp/build7315640391316178285.tmp/preproc/ctags_target.cpp", prototypes[0].File) - require.Equal(t, "void loop();", prototypes[1].Prototype) - - require.Equal(t, 16, ctx.PrototypesLineWhereToInsert) -} - -func TestCTagsToPrototypesDefaultArguments(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserDefaultArguments.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - commands := []types.Command{ - &ctags.CTagsParser{}, - &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 test(int x = 1);", prototypes[0].Prototype) - require.Equal(t, "void setup();", prototypes[1].Prototype) - require.Equal(t, "/tmp/test179252494/preproc/ctags_target.cpp", prototypes[1].File) - require.Equal(t, "void loop();", prototypes[2].Prototype) - - require.Equal(t, 2, ctx.PrototypesLineWhereToInsert) -} - -func TestCTagsToPrototypesNamespace(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserNamespace.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - commands := []types.Command{ - &ctags.CTagsParser{}, - &ctags.CTagsToPrototypes{}, - } - - for _, command := range commands { - err := command.Run(ctx) - NoError(t, err) - } - - prototypes := ctx.Prototypes - - require.Equal(t, 2, len(prototypes)) - require.Equal(t, "void setup();", prototypes[0].Prototype) - require.Equal(t, "/tmp/test030883150/preproc/ctags_target.cpp", prototypes[0].File) - require.Equal(t, "void loop();", prototypes[1].Prototype) - - require.Equal(t, 8, ctx.PrototypesLineWhereToInsert) -} - -func TestCTagsToPrototypesStatic(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserStatic.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - commands := []types.Command{ - &ctags.CTagsParser{}, - &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, "/tmp/test542833488/preproc/ctags_target.cpp", prototypes[0].File) - require.Equal(t, "void loop();", prototypes[1].Prototype) - require.Equal(t, "void doStuff();", prototypes[2].Prototype) - require.Equal(t, "static", prototypes[2].Modifiers) - - require.Equal(t, 2, ctx.PrototypesLineWhereToInsert) -} - -func TestCTagsToPrototypesFunctionPointer(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserFunctionPointer.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - commands := []types.Command{ - &ctags.CTagsParser{}, - &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 t1Callback();", prototypes[0].Prototype) - require.Equal(t, "/tmp/test547238273/preproc/ctags_target.cpp", prototypes[0].File) - require.Equal(t, "void setup();", prototypes[1].Prototype) - require.Equal(t, "void loop();", prototypes[2].Prototype) - - require.Equal(t, 2, ctx.PrototypesLineWhereToInsert) -} - -func TestCTagsToPrototypesFunctionPointers(t *testing.T) { - ctx := &types.Context{} - - bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserFunctionPointers.txt")) - NoError(t, err) - - ctx.CTagsOutput = string(bytes) - - commands := []types.Command{ - &ctags.CTagsParser{}, - &ctags.CTagsToPrototypes{}, - } - - for _, command := range commands { - err := command.Run(ctx) - NoError(t, err) - } - - prototypes := ctx.Prototypes - require.Equal(t, 2, len(prototypes)) - require.Equal(t, "void setup();", prototypes[0].Prototype) - require.Equal(t, "/tmp/test907446433/preproc/ctags_target.cpp", prototypes[0].File) - require.Equal(t, "void loop();", prototypes[1].Prototype) - - require.Equal(t, 2, ctx.PrototypesLineWhereToInsert) -} - -func TestCTagsRunnerSketchWithClassFunction(t *testing.T) { - DownloadCoresAndToolsAndLibraries(t) - - sketchLocation := Abs(t, filepath.Join("sketch_class_function", "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{}, - &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) -}