Skip to content

Commit d5d9186

Browse files
committed
Only trust function line number in main sketch file
Previously, function line numbers are extracted from any tag (in any file) so exctracted line is nonsense if the main sketch is empty/has lots of lines and lots of comments/etc Fixes arduino#199
1 parent bc2c38f commit d5d9186

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

src/arduino.cc/builder/ctags/ctags_parser.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@ var KNOWN_TAG_KINDS = map[string]bool{
5353
}
5454

5555
type CTagsParser struct {
56-
tags []*types.CTag
56+
tags []*types.CTag
57+
mainFile string
5758
}
5859

59-
func (p *CTagsParser) Parse(ctagsOutput string) []*types.CTag {
60+
func (p *CTagsParser) Parse(ctagsOutput string, mainFile string) []*types.CTag {
6061
rows := strings.Split(ctagsOutput, "\n")
6162
rows = removeEmpty(rows)
6263

64+
p.mainFile = mainFile
65+
6366
for _, row := range rows {
6467
p.tags = append(p.tags, parseTag(row))
6568
}

src/arduino.cc/builder/ctags/ctags_to_prototypes.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ func (p *CTagsParser) findLineWhereToInsertPrototypes() int {
4848
} else {
4949
return firstFunctionPointerAsArgument
5050
}
51-
} else if firstFunctionLine == -1 {
51+
} else if firstFunctionLine != -1 {
52+
return firstFunctionLine
53+
} else if firstFunctionPointerAsArgument != -1 {
5254
return firstFunctionPointerAsArgument
5355
} else {
54-
return firstFunctionLine
56+
return 0
5557
}
5658
}
5759

@@ -86,7 +88,7 @@ func (p *CTagsParser) collectFunctionNames() []string {
8688

8789
func (p *CTagsParser) firstFunctionAtLine() int {
8890
for _, tag := range p.tags {
89-
if !tagIsUnknown(tag) && isHandled(tag) && tag.Kind == KIND_FUNCTION {
91+
if !tagIsUnknown(tag) && isHandled(tag) && tag.Kind == KIND_FUNCTION && tag.Filename == p.mainFile {
9092
return tag.Line
9193
}
9294
}

src/arduino.cc/builder/ctags_runner.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ func (s *CTagsRunner) Run(ctx *types.Context) error {
7474
ctx.CTagsOutput = string(sourceBytes)
7575

7676
parser := &ctags.CTagsParser{}
77-
ctx.CTagsOfPreprocessedSource = parser.Parse(ctx.CTagsOutput)
77+
ctx.CTagsOfPreprocessedSource = parser.Parse(ctx.CTagsOutput, ctx.Sketch.MainFile.Name)
78+
7879
protos, line := parser.GeneratePrototypes()
7980
if line != -1 {
8081
ctx.PrototypesLineWhereToInsert = line

0 commit comments

Comments
 (0)