Skip to content

Solve multiline functions generation #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 14, 2017
Merged
Prev Previous commit
Next Next commit
Add step to correct tags data C linkage
  • Loading branch information
facchinm committed Nov 29, 2016
commit 991949b26695c075ea34e00f8c61d518428f7cb1
139 changes: 139 additions & 0 deletions src/arduino.cc/builder/ctags/ctags_has_issues.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* This file is part of Arduino Builder.
*
* Arduino Builder is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As a special exception, you may use this file as part of a free software
* library without restriction. Specifically, if other files instantiate
* templates or use macros or inline functions from this file, or you compile
* this file and link it with other files to produce an executable, this
* file does not by itself cause the resulting executable to be covered by
* the GNU General Public License. This exception does not however
* invalidate any other reasons why the executable file might be covered by
* the GNU General Public License.
*
* Copyright 2015 Arduino LLC (http://www.arduino.cc/)
*/

package ctags

import (
"bufio"
"os"
"strings"

"arduino.cc/builder/types"
)

func (p *CTagsParser) FixCLinkageTagsDeclarations(tags []*types.CTag) {

linesMap := p.FindCLinkageLines(tags)
for i, _ := range tags {

if sliceContainsInt(linesMap[tags[i].Filename], tags[i].Line) &&
!strings.Contains(tags[i].PrototypeModifiers, EXTERN) {
tags[i].PrototypeModifiers = tags[i].PrototypeModifiers + " " + EXTERN
}
}
}

func sliceContainsInt(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

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

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

for _, tag := range tags {

if lines[tag.Filename] != nil {
break
}

file, err := os.Open(tag.Filename)
if err == nil {
defer file.Close()

lines[tag.Filename] = append(lines[tag.Filename], 0)

scanner := bufio.NewScanner(file)

// we can't remove the comments otherwise the line number will be wrong
// there are three cases:
// 1 - extern "C" void foo()
// 2 - extern "C" {
// void foo();
// void bar();
// }
// 3 - extern "C"
// {
// void foo();
// void bar();
// }
// case 1 and 2 can be simply recognized with string matching and indent level count
// case 3 needs specia attention: if the line ONLY contains `extern "C"` string, don't bail out on indent level = 0

inScope := false
enteringScope := false
indentLevels := 0
line := 0

externCDecl := removeSpacesAndTabs(EXTERN)

for scanner.Scan() {
line++
str := removeSpacesAndTabs(scanner.Text())

if len(str) == 0 {
continue
}

// check if we are on the first non empty line after externCDecl in case 3
if enteringScope == true {
enteringScope = false
}

// check if the line contains externCDecl
if strings.Contains(str, externCDecl) {
inScope = true
if len(str) == len(externCDecl) {
// case 3
enteringScope = true
}
}
if inScope == true {
lines[tag.Filename] = append(lines[tag.Filename], line)
}
indentLevels += strings.Count(str, "{") - strings.Count(str, "}")

// Bail out if indentLevel is zero and we are not in case 3
if indentLevels == 0 && enteringScope == false {
inScope = false
}
}
}

}
return lines
}
2 changes: 2 additions & 0 deletions src/arduino.cc/builder/ctags_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func (s *CTagsRunner) Run(ctx *types.Context) error {

parser := &ctags.CTagsParser{}
ctx.CTagsOfPreprocessedSource = parser.Parse(ctx.CTagsOutput)

parser.FixCLinkageTagsDeclarations(ctx.CTagsOfPreprocessedSource)
protos, line := parser.GeneratePrototypes()
if line != -1 {
ctx.PrototypesLineWhereToInsert = line
Expand Down