Skip to content

Commit 2f78246

Browse files
author
Federico Fissore
committed
Moved CTag struct from CTagsParser to types package. Because it's not local to
CTagsParser command, CTag type must be equally accessible by all commands Signed-off-by: Federico Fissore <[email protected]>
1 parent 2d30efd commit 2f78246

6 files changed

+66
-63
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func (s *CollectCTagsFromSketchFiles) Run(context map[string]interface{}) error
4242
sketch := context[constants.CTX_SKETCH].(*types.Sketch)
4343
sketchFileNames := collectSketchFileNamesFrom(sketch)
4444

45-
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*CTag)
46-
ctagsOfSketch := []*CTag{}
45+
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
46+
ctagsOfSketch := []*types.CTag{}
4747
for _, ctag := range ctags {
4848
if utils.SliceContains(sketchFileNames, strings.Replace(ctag.Filename, "\\\\", "\\", -1)) {
4949
ctagsOfSketch = append(ctagsOfSketch, ctag)

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ package builder
3131

3232
import (
3333
"arduino.cc/builder/constants"
34+
"arduino.cc/builder/types"
3435
)
3536

3637
type ComparePrototypesFromSourceAndPreprocSource struct{}
3738

3839
func (s *ComparePrototypesFromSourceAndPreprocSource) Run(context map[string]interface{}) error {
39-
ctagsOfSource := context[constants.CTX_CTAGS_OF_SOURCE].([]*CTag)
40-
ctagsOfPreprocSource := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*CTag)
40+
ctagsOfSource := context[constants.CTX_CTAGS_OF_SOURCE].([]*types.CTag)
41+
ctagsOfPreprocSource := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
4142

42-
actualCTags := []*CTag{}
43+
actualCTags := []*types.CTag{}
4344
for _, ctagOfPreprocSource := range ctagsOfPreprocSource {
4445
if sliceContainsCTag(ctagsOfSource, ctagOfPreprocSource) {
4546
actualCTags = append(actualCTags, ctagOfPreprocSource)
@@ -51,7 +52,7 @@ func (s *ComparePrototypesFromSourceAndPreprocSource) Run(context map[string]int
5152
return nil
5253
}
5354

54-
func sliceContainsCTag(slice []*CTag, target *CTag) bool {
55+
func sliceContainsCTag(slice []*types.CTag, target *types.CTag) bool {
5556
for _, value := range slice {
5657
if value.FunctionName == target.FunctionName {
5758
return true

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

+17-35
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ package builder
3131

3232
import (
3333
"arduino.cc/builder/constants"
34+
"arduino.cc/builder/types"
3435
"arduino.cc/builder/utils"
3536
"os"
3637
"reflect"
@@ -54,31 +55,12 @@ var KNOWN_TAG_KINDS = map[string]bool{
5455

5556
type CTagsParser struct{}
5657

57-
type CTag struct {
58-
FunctionName string
59-
Kind string
60-
Line int
61-
Signature string
62-
Returntype string
63-
Code string
64-
Class string
65-
Struct string
66-
Namespace string
67-
Filename string
68-
Typeref string
69-
SkipMe bool
70-
71-
Prototype string
72-
Function string
73-
PrototypeModifiers string
74-
}
75-
7658
func (s *CTagsParser) Run(context map[string]interface{}) error {
7759
rows := strings.Split(context[constants.CTX_CTAGS_OUTPUT].(string), "\n")
7860

7961
rows = removeEmpty(rows)
8062

81-
var tags []*CTag
63+
var tags []*types.CTag
8264
for _, row := range rows {
8365
tags = append(tags, parseTag(row))
8466
}
@@ -96,15 +78,15 @@ func (s *CTagsParser) Run(context map[string]interface{}) error {
9678
return nil
9779
}
9880

99-
func addPrototypes(tags []*CTag) {
81+
func addPrototypes(tags []*types.CTag) {
10082
for _, tag := range tags {
10183
if !tag.SkipMe {
102-
tag.AddPrototype()
84+
addPrototype(tag)
10385
}
10486
}
10587
}
10688

107-
func (tag *CTag) AddPrototype() {
89+
func addPrototype(tag *types.CTag) {
10890
if strings.Index(tag.Returntype, TEMPLATE) == 0 || strings.Index(tag.Code, TEMPLATE) == 0 {
10991
code := tag.Code
11092
if strings.Contains(code, "{") {
@@ -125,7 +107,7 @@ func (tag *CTag) AddPrototype() {
125107
tag.PrototypeModifiers = strings.TrimSpace(tag.PrototypeModifiers)
126108
}
127109

128-
func removeDefinedProtypes(tags []*CTag, context map[string]interface{}) {
110+
func removeDefinedProtypes(tags []*types.CTag, context map[string]interface{}) {
129111
definedPrototypes := make(map[string]bool)
130112
for _, tag := range tags {
131113
if tag.Kind == KIND_PROTOTYPE {
@@ -143,7 +125,7 @@ func removeDefinedProtypes(tags []*CTag, context map[string]interface{}) {
143125
}
144126
}
145127

146-
func removeDuplicate(tags []*CTag) {
128+
func removeDuplicate(tags []*types.CTag) {
147129
definedPrototypes := make(map[string]bool)
148130

149131
for _, tag := range tags {
@@ -155,9 +137,9 @@ func removeDuplicate(tags []*CTag) {
155137
}
156138
}
157139

158-
type skipFuncType func(tag *CTag) bool
140+
type skipFuncType func(tag *types.CTag) bool
159141

160-
func skipTagsWhere(tags []*CTag, skipFunc skipFuncType, context map[string]interface{}) {
142+
func skipTagsWhere(tags []*types.CTag, skipFunc skipFuncType, context map[string]interface{}) {
161143
for _, tag := range tags {
162144
if !tag.SkipMe {
163145
skip := skipFunc(tag)
@@ -169,11 +151,11 @@ func skipTagsWhere(tags []*CTag, skipFunc skipFuncType, context map[string]inter
169151
}
170152
}
171153

172-
func signatureContainsDefaultArg(tag *CTag) bool {
154+
func signatureContainsDefaultArg(tag *types.CTag) bool {
173155
return strings.Contains(tag.Signature, "=")
174156
}
175157

176-
func prototypeAndCodeDontMatch(tag *CTag) bool {
158+
func prototypeAndCodeDontMatch(tag *types.CTag) bool {
177159
if tag.SkipMe {
178160
return true
179161
}
@@ -195,11 +177,11 @@ func removeSpacesAndTabs(s string) string {
195177
return s
196178
}
197179

198-
func tagIsUnhandled(tag *CTag) bool {
199-
return !tag.IsHandled()
180+
func tagIsUnhandled(tag *types.CTag) bool {
181+
return !isHandled(tag)
200182
}
201183

202-
func (tag *CTag) IsHandled() bool {
184+
func isHandled(tag *types.CTag) bool {
203185
if tag.Class != "" {
204186
return false
205187
}
@@ -212,12 +194,12 @@ func (tag *CTag) IsHandled() bool {
212194
return true
213195
}
214196

215-
func tagIsUnknown(tag *CTag) bool {
197+
func tagIsUnknown(tag *types.CTag) bool {
216198
return !KNOWN_TAG_KINDS[tag.Kind]
217199
}
218200

219-
func parseTag(row string) *CTag {
220-
tag := &CTag{}
201+
func parseTag(row string) *types.CTag {
202+
tag := &types.CTag{}
221203
parts := strings.Split(row, "\t")
222204

223205
tag.FunctionName = parts[0]

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
type CTagsToPrototypes struct{}
3939

4040
func (s *CTagsToPrototypes) Run(context map[string]interface{}) error {
41-
tags := context[constants.CTX_COLLECTED_CTAGS].([]*CTag)
41+
tags := context[constants.CTX_COLLECTED_CTAGS].([]*types.CTag)
4242

4343
lineWhereToInsertPrototypes := findLineWhereToInsertPrototypes(tags)
4444
if lineWhereToInsertPrototypes != -1 {
@@ -51,7 +51,7 @@ func (s *CTagsToPrototypes) Run(context map[string]interface{}) error {
5151
return nil
5252
}
5353

54-
func findLineWhereToInsertPrototypes(tags []*CTag) int {
54+
func findLineWhereToInsertPrototypes(tags []*types.CTag) int {
5555
firstFunctionLine := firstFunctionAtLine(tags)
5656
firstFunctionPointerAsArgument := firstFunctionPointerUsedAsArgument(tags)
5757
if firstFunctionLine != -1 && firstFunctionPointerAsArgument != -1 {
@@ -67,7 +67,7 @@ func findLineWhereToInsertPrototypes(tags []*CTag) int {
6767
}
6868
}
6969

70-
func firstFunctionPointerUsedAsArgument(tags []*CTag) int {
70+
func firstFunctionPointerUsedAsArgument(tags []*types.CTag) int {
7171
functionNames := collectFunctionNames(tags)
7272
for _, tag := range tags {
7373
if functionNameUsedAsFunctionPointerIn(tag, functionNames) {
@@ -77,7 +77,7 @@ func firstFunctionPointerUsedAsArgument(tags []*CTag) int {
7777
return -1
7878
}
7979

80-
func functionNameUsedAsFunctionPointerIn(tag *CTag, functionNames []string) bool {
80+
func functionNameUsedAsFunctionPointerIn(tag *types.CTag, functionNames []string) bool {
8181
for _, functionName := range functionNames {
8282
if strings.Index(tag.Code, "&"+functionName) != -1 {
8383
return true
@@ -86,7 +86,7 @@ func functionNameUsedAsFunctionPointerIn(tag *CTag, functionNames []string) bool
8686
return false
8787
}
8888

89-
func collectFunctionNames(tags []*CTag) []string {
89+
func collectFunctionNames(tags []*types.CTag) []string {
9090
names := []string{}
9191
for _, tag := range tags {
9292
if tag.Kind == KIND_FUNCTION {
@@ -96,16 +96,16 @@ func collectFunctionNames(tags []*CTag) []string {
9696
return names
9797
}
9898

99-
func firstFunctionAtLine(tags []*CTag) int {
99+
func firstFunctionAtLine(tags []*types.CTag) int {
100100
for _, tag := range tags {
101-
if !tagIsUnknown(tag) && tag.IsHandled() && tag.Kind == KIND_FUNCTION {
101+
if !tagIsUnknown(tag) && isHandled(tag) && tag.Kind == KIND_FUNCTION {
102102
return tag.Line
103103
}
104104
}
105105
return -1
106106
}
107107

108-
func toPrototypes(tags []*CTag) []*types.Prototype {
108+
func toPrototypes(tags []*types.CTag) []*types.Prototype {
109109
prototypes := []*types.Prototype{}
110110
for _, tag := range tags {
111111
if !tag.SkipMe {

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

+15-14
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+
"arduino.cc/builder/types"
3536
"github.com/stretchr/testify/require"
3637
"io/ioutil"
3738
"path/filepath"
@@ -49,7 +50,7 @@ func TestCTagsParserShouldListPrototypes(t *testing.T) {
4950
ctagsParser := builder.CTagsParser{}
5051
ctagsParser.Run(context)
5152

52-
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*builder.CTag)
53+
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
5354

5455
require.Equal(t, 8, len(ctags))
5556
idx := 0
@@ -97,7 +98,7 @@ func TestCTagsParserShouldListTemplates(t *testing.T) {
9798
ctagsParser := builder.CTagsParser{}
9899
ctagsParser.Run(context)
99100

100-
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*builder.CTag)
101+
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
101102

102103
require.Equal(t, 3, len(ctags))
103104
idx := 0
@@ -123,7 +124,7 @@ func TestCTagsParserShouldListTemplates2(t *testing.T) {
123124
ctagsParser := builder.CTagsParser{}
124125
ctagsParser.Run(context)
125126

126-
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*builder.CTag)
127+
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
127128

128129
require.Equal(t, 4, len(ctags))
129130
idx := 0
@@ -153,7 +154,7 @@ func TestCTagsParserShouldDealWithClasses(t *testing.T) {
153154
ctagsParser := builder.CTagsParser{}
154155
ctagsParser.Run(context)
155156

156-
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*builder.CTag)
157+
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
157158

158159
require.Equal(t, 2, len(ctags))
159160
idx := 0
@@ -175,7 +176,7 @@ func TestCTagsParserShouldDealWithStructs(t *testing.T) {
175176
ctagsParser := builder.CTagsParser{}
176177
ctagsParser.Run(context)
177178

178-
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*builder.CTag)
179+
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
179180

180181
require.Equal(t, 5, len(ctags))
181182
idx := 0
@@ -207,7 +208,7 @@ func TestCTagsParserShouldDealWithMacros(t *testing.T) {
207208
ctagsParser := builder.CTagsParser{}
208209
ctagsParser.Run(context)
209210

210-
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*builder.CTag)
211+
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
211212

212213
require.Equal(t, 8, len(ctags))
213214
idx := 0
@@ -247,7 +248,7 @@ func TestCTagsParserShouldDealFunctionWithDifferentSignatures(t *testing.T) {
247248
ctagsParser := builder.CTagsParser{}
248249
ctagsParser.Run(context)
249250

250-
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*builder.CTag)
251+
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
251252

252253
require.Equal(t, 3, len(ctags))
253254
idx := 0
@@ -272,7 +273,7 @@ func TestCTagsParserClassMembersAreFilteredOut(t *testing.T) {
272273
ctagsParser := builder.CTagsParser{}
273274
ctagsParser.Run(context)
274275

275-
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*builder.CTag)
276+
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
276277

277278
require.Equal(t, 5, len(ctags))
278279
idx := 0
@@ -306,7 +307,7 @@ func TestCTagsParserStructWithFunctions(t *testing.T) {
306307
ctagsParser := builder.CTagsParser{}
307308
ctagsParser.Run(context)
308309

309-
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*builder.CTag)
310+
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
310311

311312
require.Equal(t, 8, len(ctags))
312313
idx := 0
@@ -348,7 +349,7 @@ func TestCTagsParserDefaultArguments(t *testing.T) {
348349
ctagsParser := builder.CTagsParser{}
349350
ctagsParser.Run(context)
350351

351-
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*builder.CTag)
352+
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
352353

353354
require.Equal(t, 3, len(ctags))
354355
idx := 0
@@ -374,7 +375,7 @@ func TestCTagsParserNamespace(t *testing.T) {
374375
ctagsParser := builder.CTagsParser{}
375376
ctagsParser.Run(context)
376377

377-
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*builder.CTag)
378+
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
378379

379380
require.Equal(t, 3, len(ctags))
380381
idx := 0
@@ -400,7 +401,7 @@ func TestCTagsParserStatic(t *testing.T) {
400401
ctagsParser := builder.CTagsParser{}
401402
ctagsParser.Run(context)
402403

403-
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*builder.CTag)
404+
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
404405

405406
require.Equal(t, 3, len(ctags))
406407
idx := 0
@@ -425,7 +426,7 @@ func TestCTagsParserFunctionPointer(t *testing.T) {
425426
ctagsParser := builder.CTagsParser{}
426427
ctagsParser.Run(context)
427428

428-
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*builder.CTag)
429+
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
429430

430431
require.Equal(t, 4, len(ctags))
431432
idx := 0
@@ -453,7 +454,7 @@ func TestCTagsParserFunctionPointers(t *testing.T) {
453454
ctagsParser := builder.CTagsParser{}
454455
ctagsParser.Run(context)
455456

456-
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*builder.CTag)
457+
ctags := context[constants.CTX_CTAGS_OF_PREPROC_SOURCE].([]*types.CTag)
457458

458459
require.Equal(t, 5, len(ctags))
459460
idx := 0

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

+19
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,22 @@ type LibraryResolutionResult struct {
175175
IsLibraryFromPlatform bool
176176
NotUsedLibraries []*Library
177177
}
178+
179+
type CTag struct {
180+
FunctionName string
181+
Kind string
182+
Line int
183+
Signature string
184+
Returntype string
185+
Code string
186+
Class string
187+
Struct string
188+
Namespace string
189+
Filename string
190+
Typeref string
191+
SkipMe bool
192+
193+
Prototype string
194+
Function string
195+
PrototypeModifiers string
196+
}

0 commit comments

Comments
 (0)