Skip to content

Commit 9b3f6b8

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 #199
1 parent 287a9c8 commit 9b3f6b8

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-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

@@ -90,7 +92,7 @@ func (p *CTagsParser) collectFunctions() []*types.CTag {
9092

9193
func (p *CTagsParser) firstFunctionAtLine() int {
9294
for _, tag := range p.tags {
93-
if !tagIsUnknown(tag) && isHandled(tag) && tag.Kind == KIND_FUNCTION {
95+
if !tagIsUnknown(tag) && isHandled(tag) && tag.Kind == KIND_FUNCTION && tag.Filename == p.mainFile {
9496
return tag.Line
9597
}
9698
}

src/arduino.cc/builder/ctags_runner.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ 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+
79+
parser.FixCLinkageTagsDeclarations(ctx.CTagsOfPreprocessedSource)
80+
7881
protos, line := parser.GeneratePrototypes()
7982
if line != -1 {
8083
ctx.PrototypesLineWhereToInsert = line

0 commit comments

Comments
 (0)