Skip to content

Commit 39042ea

Browse files
committed
Fixed linter warnings
1 parent d5fa620 commit 39042ea

File tree

7 files changed

+59
-55
lines changed

7 files changed

+59
-55
lines changed

Diff for: arduino/builder/preprocessor/ctags.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func PreprocessSketchWithCtags(sketch *sketch.Sketch, buildPath *paths.Path, inc
9090
}
9191

9292
// Parse CTags output
93-
parser := &ctags.CTagsParser{}
93+
parser := &ctags.Parser{}
9494
prototypes, firstFunctionLine := parser.Parse(ctagsOutput, sketch.MainFile)
9595
if firstFunctionLine == -1 {
9696
firstFunctionLine = 0
@@ -175,6 +175,7 @@ func isFirstFunctionOutsideOfSource(firstFunctionLine int, sourceRows []string)
175175
return firstFunctionLine > len(sourceRows)-1
176176
}
177177

178+
// RunCTags performs a run of ctags on the given source file. Returns the ctags output and the stderr contents.
178179
func RunCTags(sourceFile *paths.Path, buildProperties *properties.Map) ([]byte, []byte, error) {
179180
ctagsBuildProperties := properties.NewMap()
180181
ctagsBuildProperties.Set("tools.ctags.path", "{runtime.tools.ctags.path}")

Diff for: arduino/builder/preprocessor/gcc.go

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828
"github.com/pkg/errors"
2929
)
3030

31+
// GCC performs a run of the gcc preprocess (macro/includes expansion). The function output the result
32+
// to targetFilePath. Returns the stdout/stderr of gcc if any.
3133
func GCC(sourceFilePath *paths.Path, targetFilePath *paths.Path, includes paths.PathList, buildProperties *properties.Map) ([]byte, []byte, error) {
3234
gccBuildProperties := properties.NewMap()
3335
gccBuildProperties.Set("preproc.macros.flags", "-w -x c++ -E -CC")

Diff for: arduino/builder/preprocessor/internal/ctags/ctags_has_issues.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ import (
2323
"golang.org/x/exp/slices"
2424
)
2525

26-
func (p *CTagsParser) fixCLinkageTagsDeclarations() {
26+
func (p *Parser) fixCLinkageTagsDeclarations() {
2727
linesMap := p.FindCLinkageLines(p.tags)
2828
for i := range p.tags {
2929
if slices.Contains(linesMap[p.tags[i].Filename], p.tags[i].Line) &&
30-
!strings.Contains(p.tags[i].PrototypeModifiers, EXTERN) {
31-
p.tags[i].PrototypeModifiers = p.tags[i].PrototypeModifiers + " " + EXTERN
30+
!strings.Contains(p.tags[i].PrototypeModifiers, keywordExternC) {
31+
p.tags[i].PrototypeModifiers = p.tags[i].PrototypeModifiers + " " + keywordExternC
3232
}
3333
}
3434
}
3535

36-
func (p *CTagsParser) prototypeAndCodeDontMatch(tag *CTag) bool {
36+
func (p *Parser) prototypeAndCodeDontMatch(tag *Tag) bool {
3737
if tag.SkipMe {
3838
return true
3939
}
@@ -98,7 +98,7 @@ func (p *CTagsParser) prototypeAndCodeDontMatch(tag *CTag) bool {
9898
return ret == -1
9999
}
100100

101-
func findTemplateMultiline(tag *CTag) string {
101+
func findTemplateMultiline(tag *Tag) string {
102102
code, _ := getFunctionProtoUntilTemplateToken(tag, tag.Code)
103103
return removeEverythingAfterClosingRoundBracket(code)
104104
}
@@ -108,7 +108,7 @@ func removeEverythingAfterClosingRoundBracket(s string) string {
108108
return s[0 : n+1]
109109
}
110110

111-
func getFunctionProtoUntilTemplateToken(tag *CTag, code string) (string, int) {
111+
func getFunctionProtoUntilTemplateToken(tag *Tag, code string) (string, int) {
112112

113113
/* FIXME I'm ugly */
114114
line := 0
@@ -128,7 +128,7 @@ func getFunctionProtoUntilTemplateToken(tag *CTag, code string) (string, int) {
128128
textBuffer = append(textBuffer, text)
129129
}
130130

131-
for line > 0 && !strings.Contains(code, TEMPLATE) {
131+
for line > 0 && !strings.Contains(code, keywordTemplate) {
132132

133133
line = line - 1
134134
text := textBuffer[line]
@@ -141,7 +141,7 @@ func getFunctionProtoUntilTemplateToken(tag *CTag, code string) (string, int) {
141141
return code, line
142142
}
143143

144-
func getFunctionProtoWithNPreviousCharacters(tag *CTag, code string, n int) (string, int) {
144+
func getFunctionProtoWithNPreviousCharacters(tag *Tag, code string, n int) (string, int) {
145145

146146
/* FIXME I'm ugly */
147147
expectedPrototypeLen := len(code) + n
@@ -204,10 +204,9 @@ func removeComments(text string, multilinecomment bool) (string, bool) {
204204
return text, multilinecomment
205205
}
206206

207-
/* This function scans the source files searching for "extern C" context
208-
* It save the line numbers in a map filename -> {lines...}
209-
*/
210-
func (p *CTagsParser) FindCLinkageLines(tags []*CTag) map[string][]int {
207+
// FindCLinkageLines scans the source files searching for "extern C" context
208+
// It save the line numbers in a map filename -> {lines...}
209+
func (p *Parser) FindCLinkageLines(tags []*Tag) map[string][]int {
211210

212211
lines := make(map[string][]int)
213212

@@ -245,7 +244,7 @@ func (p *CTagsParser) FindCLinkageLines(tags []*CTag) map[string][]int {
245244
indentLevels := 0
246245
line := 0
247246

248-
externCDecl := removeSpacesAndTabs(EXTERN)
247+
externCDecl := removeSpacesAndTabs(keywordExternC)
249248

250249
for scanner.Scan() {
251250
line++

Diff for: arduino/builder/preprocessor/internal/ctags/ctags_parser.go

+30-27
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,28 @@ import (
2222
"github.com/arduino/go-paths-helper"
2323
)
2424

25-
const KIND_PROTOTYPE = "prototype"
26-
const KIND_FUNCTION = "function"
25+
const kindPrototype = "prototype"
26+
const kindFunction = "function"
2727

2828
//const KIND_PROTOTYPE_MODIFIERS = "prototype_modifiers"
2929

30-
const TEMPLATE = "template"
31-
const STATIC = "static"
32-
const EXTERN = "extern \"C\""
30+
const keywordTemplate = "template"
31+
const keywordStatic = "static"
32+
const keywordExternC = "extern \"C\""
3333

34-
var KNOWN_TAG_KINDS = map[string]bool{
34+
var knownTagKinds = map[string]bool{
3535
"prototype": true,
3636
"function": true,
3737
}
3838

39-
type CTagsParser struct {
40-
tags []*CTag
39+
// Parser is a parser for ctags output
40+
type Parser struct {
41+
tags []*Tag
4142
mainFile *paths.Path
4243
}
4344

44-
type CTag struct {
45+
// Tag is a tag generated by ctags
46+
type Tag struct {
4547
FunctionName string
4648
Kind string
4749
Line int
@@ -58,7 +60,8 @@ type CTag struct {
5860
PrototypeModifiers string
5961
}
6062

61-
func (p *CTagsParser) Parse(ctagsOutput []byte, mainFile *paths.Path) ([]*Prototype, int) {
63+
// Parse a ctags output and generates Prototypes
64+
func (p *Parser) Parse(ctagsOutput []byte, mainFile *paths.Path) ([]*Prototype, int) {
6265
rows := strings.Split(string(ctagsOutput), "\n")
6366
rows = removeEmpty(rows)
6467

@@ -79,17 +82,17 @@ func (p *CTagsParser) Parse(ctagsOutput []byte, mainFile *paths.Path) ([]*Protot
7982
return p.toPrototypes(), p.findLineWhereToInsertPrototypes()
8083
}
8184

82-
func (p *CTagsParser) addPrototypes() {
85+
func (p *Parser) addPrototypes() {
8386
for _, tag := range p.tags {
8487
if !tag.SkipMe {
8588
addPrototype(tag)
8689
}
8790
}
8891
}
8992

90-
func addPrototype(tag *CTag) {
91-
if strings.Index(tag.Prototype, TEMPLATE) == 0 {
92-
if strings.Index(tag.Code, TEMPLATE) == 0 {
93+
func addPrototype(tag *Tag) {
94+
if strings.Index(tag.Prototype, keywordTemplate) == 0 {
95+
if strings.Index(tag.Code, keywordTemplate) == 0 {
9396
code := tag.Code
9497
if strings.Contains(code, "{") {
9598
code = code[:strings.Index(code, "{")]
@@ -106,19 +109,19 @@ func addPrototype(tag *CTag) {
106109
}
107110

108111
tag.PrototypeModifiers = ""
109-
if strings.Contains(tag.Code, STATIC+" ") {
110-
tag.PrototypeModifiers = tag.PrototypeModifiers + " " + STATIC
112+
if strings.Contains(tag.Code, keywordStatic+" ") {
113+
tag.PrototypeModifiers = tag.PrototypeModifiers + " " + keywordStatic
111114
}
112115

113116
// Extern "C" modifier is now added in FixCLinkageTagsDeclarations
114117

115118
tag.PrototypeModifiers = strings.TrimSpace(tag.PrototypeModifiers)
116119
}
117120

118-
func (p *CTagsParser) removeDefinedProtypes() {
121+
func (p *Parser) removeDefinedProtypes() {
119122
definedPrototypes := make(map[string]bool)
120123
for _, tag := range p.tags {
121-
if tag.Kind == KIND_PROTOTYPE {
124+
if tag.Kind == kindPrototype {
122125
definedPrototypes[tag.Prototype] = true
123126
}
124127
}
@@ -133,7 +136,7 @@ func (p *CTagsParser) removeDefinedProtypes() {
133136
}
134137
}
135138

136-
func (p *CTagsParser) skipDuplicates() {
139+
func (p *Parser) skipDuplicates() {
137140
definedPrototypes := make(map[string]bool)
138141

139142
for _, tag := range p.tags {
@@ -145,9 +148,9 @@ func (p *CTagsParser) skipDuplicates() {
145148
}
146149
}
147150

148-
type skipFuncType func(tag *CTag) bool
151+
type skipFuncType func(tag *Tag) bool
149152

150-
func (p *CTagsParser) skipTagsWhere(skipFunc skipFuncType) {
153+
func (p *Parser) skipTagsWhere(skipFunc skipFuncType) {
151154
for _, tag := range p.tags {
152155
if !tag.SkipMe {
153156
skip := skipFunc(tag)
@@ -169,11 +172,11 @@ func removeSpacesAndTabs(s string) string {
169172
return s
170173
}
171174

172-
func tagIsUnhandled(tag *CTag) bool {
175+
func tagIsUnhandled(tag *Tag) bool {
173176
return !isHandled(tag)
174177
}
175178

176-
func isHandled(tag *CTag) bool {
179+
func isHandled(tag *Tag) bool {
177180
if tag.Class != "" {
178181
return false
179182
}
@@ -186,12 +189,12 @@ func isHandled(tag *CTag) bool {
186189
return true
187190
}
188191

189-
func tagIsUnknown(tag *CTag) bool {
190-
return !KNOWN_TAG_KINDS[tag.Kind]
192+
func tagIsUnknown(tag *Tag) bool {
193+
return !knownTagKinds[tag.Kind]
191194
}
192195

193-
func parseTag(row string) *CTag {
194-
tag := &CTag{}
196+
func parseTag(row string) *Tag {
197+
tag := &Tag{}
195198
parts := strings.Split(row, "\t")
196199

197200
tag.FunctionName = parts[0]

Diff for: arduino/builder/preprocessor/internal/ctags/ctags_parser_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import (
2424
"github.com/stretchr/testify/require"
2525
)
2626

27-
func produceTags(t *testing.T, filename string) []*CTag {
27+
func produceTags(t *testing.T, filename string) []*Tag {
2828
bytes, err := os.ReadFile(filepath.Join("testdata", filename))
2929
require.NoError(t, err)
3030

31-
parser := CTagsParser{}
31+
parser := Parser{}
3232
parser.Parse(bytes, paths.New("sketch.ino"))
3333
return parser.tags
3434
}

Diff for: arduino/builder/preprocessor/internal/ctags/ctags_to_prototypes.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ func (proto *Prototype) String() string {
3333
return proto.Modifiers + " " + proto.Prototype + " @ " + strconv.Itoa(proto.Line)
3434
}
3535

36-
func (p *CTagsParser) findLineWhereToInsertPrototypes() int {
36+
func (p *Parser) findLineWhereToInsertPrototypes() int {
3737
firstFunctionLine := p.firstFunctionAtLine()
3838
firstFunctionPointerAsArgument := p.firstFunctionPointerUsedAsArgument()
3939
if firstFunctionLine != -1 && firstFunctionPointerAsArgument != -1 {
4040
if firstFunctionLine < firstFunctionPointerAsArgument {
4141
return firstFunctionLine
42-
} else {
43-
return firstFunctionPointerAsArgument
4442
}
43+
return firstFunctionPointerAsArgument
4544
} else if firstFunctionLine != -1 {
4645
return firstFunctionLine
4746
} else if firstFunctionPointerAsArgument != -1 {
@@ -51,7 +50,7 @@ func (p *CTagsParser) findLineWhereToInsertPrototypes() int {
5150
}
5251
}
5352

54-
func (p *CTagsParser) firstFunctionPointerUsedAsArgument() int {
53+
func (p *Parser) firstFunctionPointerUsedAsArgument() int {
5554
functionTags := p.collectFunctions()
5655
for _, tag := range p.tags {
5756
if functionNameUsedAsFunctionPointerIn(tag, functionTags) {
@@ -61,7 +60,7 @@ func (p *CTagsParser) firstFunctionPointerUsedAsArgument() int {
6160
return -1
6261
}
6362

64-
func functionNameUsedAsFunctionPointerIn(tag *CTag, functionTags []*CTag) bool {
63+
func functionNameUsedAsFunctionPointerIn(tag *Tag, functionTags []*Tag) bool {
6564
for _, functionTag := range functionTags {
6665
if tag.Line != functionTag.Line && strings.Contains(tag.Code, "&"+functionTag.FunctionName) {
6766
return true
@@ -73,26 +72,26 @@ func functionNameUsedAsFunctionPointerIn(tag *CTag, functionTags []*CTag) bool {
7372
return false
7473
}
7574

76-
func (p *CTagsParser) collectFunctions() []*CTag {
77-
functionTags := []*CTag{}
75+
func (p *Parser) collectFunctions() []*Tag {
76+
functionTags := []*Tag{}
7877
for _, tag := range p.tags {
79-
if tag.Kind == KIND_FUNCTION && !tag.SkipMe {
78+
if tag.Kind == kindFunction && !tag.SkipMe {
8079
functionTags = append(functionTags, tag)
8180
}
8281
}
8382
return functionTags
8483
}
8584

86-
func (p *CTagsParser) firstFunctionAtLine() int {
85+
func (p *Parser) firstFunctionAtLine() int {
8786
for _, tag := range p.tags {
88-
if !tagIsUnknown(tag) && isHandled(tag) && tag.Kind == KIND_FUNCTION && tag.Filename == p.mainFile.String() {
87+
if !tagIsUnknown(tag) && isHandled(tag) && tag.Kind == kindFunction && tag.Filename == p.mainFile.String() {
8988
return tag.Line
9089
}
9190
}
9291
return -1
9392
}
9493

95-
func (p *CTagsParser) toPrototypes() []*Prototype {
94+
func (p *Parser) toPrototypes() []*Prototype {
9695
prototypes := []*Prototype{}
9796
for _, tag := range p.tags {
9897
if strings.TrimSpace(tag.Prototype) == "" {

Diff for: arduino/builder/preprocessor/internal/ctags/ctags_to_prototypes_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func producePrototypes(t *testing.T, filename string, mainFile string) ([]*Proto
2828
bytes, err := os.ReadFile(filepath.Join("testdata", filename))
2929
require.NoError(t, err)
3030

31-
parser := &CTagsParser{}
31+
parser := &Parser{}
3232
return parser.Parse(bytes, paths.New(mainFile))
3333
}
3434

0 commit comments

Comments
 (0)