Skip to content

Commit 3081e19

Browse files
author
Federico Fissore
committed
Comparing prototypes with original functions. If they don't match,
prototype is skipped Signed-off-by: Federico Fissore <[email protected]>
1 parent de7d31e commit 3081e19

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/arduino.cc/builder/ctags_parser.go

+19
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func (s *CTagsParser) Run(context map[string]interface{}) error {
7979
tags = addPrototypes(tags)
8080
tags = removeDefinedProtypes(tags)
8181
tags = removeDuplicate(tags)
82+
tags = skipTagsWhere(tags, prototypeAndCodeDontMatch)
8283

8384
if len(tags) > 0 {
8485
line, err := strconv.Atoi(tags[0][FIELD_LINE])
@@ -181,6 +182,24 @@ func signatureContainsDefaultArg(tag map[string]string) bool {
181182
return strings.Contains(tag[FIELD_SIGNATURE], "=")
182183
}
183184

185+
func prototypeAndCodeDontMatch(tag map[string]string) bool {
186+
if tag[FIELD_SKIP] == "true" {
187+
return true
188+
}
189+
190+
code := removeSpacesAndTabs(tag[FIELD_CODE])
191+
prototype := removeSpacesAndTabs(tag[KIND_PROTOTYPE])
192+
prototype = prototype[0 : len(prototype)-1]
193+
194+
return strings.Index(code, prototype) == -1
195+
}
196+
197+
func removeSpacesAndTabs(s string) string {
198+
s = strings.Replace(s, " ", "", -1)
199+
s = strings.Replace(s, "\t", "", -1)
200+
return s
201+
}
202+
184203
func filterOutTagsWithField(tags []map[string]string, field string) []map[string]string {
185204
var newTags []map[string]string
186205
for _, tag := range tags {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
setup /tmp/test907446433/preproc/ctags_target.cpp /^void setup(){$/;" kind:function line:2 signature:() returntype:void
2+
loop /tmp/test907446433/preproc/ctags_target.cpp /^void loop(){}$/;" kind:function line:5 signature:() returntype:void
3+
func /tmp/test907446433/preproc/ctags_target.cpp /^void (*func())(){$/;" kind:function line:7 signature:() returntype:void
4+
funcArr /tmp/test907446433/preproc/ctags_target.cpp /^int (&funcArr())[5]{$/;" kind:function line:11 signature:() returntype:int
5+
funcCombo /tmp/test907446433/preproc/ctags_target.cpp /^void (*(&funcCombo(void (*(&in)[5])(int)))[5])(int){$/;" kind:function line:15 signature:(void (*(&in)[5])(int)) returntype:void

src/arduino.cc/builder/test/ctags_parser_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,21 @@ func TestCTagsParserStatic(t *testing.T) {
263263
require.Equal(t, "void doStuff();", prototypes[2].Prototype)
264264
require.Equal(t, "static", prototypes[2].Modifiers)
265265
}
266+
267+
func TestCTagsParserFunctionPointers(t *testing.T) {
268+
context := make(map[string]interface{})
269+
270+
bytes, err := ioutil.ReadFile(filepath.Join("ctags_output", "TestCTagsParserFunctionPointers.txt"))
271+
NoError(t, err)
272+
273+
context[constants.CTX_CTAGS_OUTPUT] = string(bytes)
274+
275+
ctagsParser := builder.CTagsParser{PrototypesField: constants.CTX_PROTOTYPES}
276+
ctagsParser.Run(context)
277+
278+
prototypes := context[constants.CTX_PROTOTYPES].([]*types.Prototype)
279+
280+
require.Equal(t, 2, len(prototypes))
281+
require.Equal(t, "void setup();", prototypes[0].Prototype)
282+
require.Equal(t, "void loop();", prototypes[1].Prototype)
283+
}

0 commit comments

Comments
 (0)