Commit 29f9bb9a authored by Hugo Moreau's avatar Hugo Moreau
Browse files

Tools: Catching formulae's variable name

 * main.go,
   tools/formulae.go : Modified.
parent 56fc2025
Pipeline #28373 passed with stage
in 2 minutes and 26 seconds
......@@ -104,7 +104,7 @@ func compileTo(filepath string, src []byte, file *os.File,
localvariable := &transform.LocalVariableAssignments{Global: true}
res := transform.ApplyAll(f, meta, []transform.Transform{
// &transform.Dependencies{},
&transform.DependenciesRework{},
// &transform.DependenciesRework{},
// &transform.Debug{}, // only for debuging a special transformation
&transform.Alive{},
&transform.Channel{
......@@ -644,14 +644,14 @@ func main() {
configJson = tools.GenerateEmptyConfig()
}
if *formulae != "" {
*ignorefunc = tools.Formulae(flag.Args()[0], *formulae)
*ignorefunc, _ = tools.Formulae(flag.Args()[0], *formulae)
configJson = tools.GenerateConfig(*formulae)
}
if *ignorefunc != "" {
if *formulae == "" && len(flag.Args()) >= 2 && *ignorefunc == "auto" {
*ignorefunc = tools.Formula(flag.Args()[0], flag.Args()[1])
*ignorefunc, _ = tools.Formula(flag.Args()[0], flag.Args()[1])
}
var unspacify = regexp.MustCompile(` *`)
s := unspacify.ReplaceAllString(*ignorefunc, "")
......
......@@ -9,56 +9,59 @@ import (
"strings"
)
func Formula(inputfile, formula string) string {
type FormulaInfo struct {
CallMap map[string]*FuncInfo
NFunc map[string]bool
NVariables map[string]bool
}
func (f *FormulaInfo) init(inputfile string) {
f.NFunc = make(map[string]bool)
f.NVariables = make(map[string]bool)
src, err := ioutil.ReadFile(inputfile)
if err != nil {
fmt.Println(os.Stderr, err)
os.Exit(2)
}
callMap := BuildCallgraph(string(src))
neededFuncMap := make(map[string]bool)
for key, _ := range callMap {
neededFuncMap[key] = false
f.CallMap = BuildCallgraph(string(src))
for key, _ := range f.CallMap {
f.NFunc[key] = false
}
neededFuncMap["main"] = true
f.NFunc["main"] = true
}
func Formula(inputfile, formula string) (string, map[string]bool) {
f := FormulaInfo{}
f.init(inputfile)
r, _ := regexp.Compile("\".*?\"")
parseFormulaeLine(formula, r, neededFuncMap)
f.parseFormulaeLine(formula, r)
notsameway := []string{}
callerNeeded("main", callMap, neededFuncMap, notsameway)
f.callerNeeded("main", notsameway)
blackbox := ""
for key, val := range neededFuncMap {
for key, val := range f.NFunc {
if !val {
blackbox += key + ";"
}
}
return blackbox
return blackbox, f.NVariables
}
func Formulae(inputfile, formulae string) string {
src, err := ioutil.ReadFile(inputfile)
if err != nil {
fmt.Println(os.Stderr, err)
os.Exit(2)
}
callMap := BuildCallgraph(string(src))
neededFuncMap := make(map[string]bool)
for key, _ := range callMap {
neededFuncMap[key] = false
}
neededFuncMap["main"] = true
getNeededFunc(formulae, neededFuncMap)
func Formulae(inputfile, formulae string) (string, map[string]bool) {
f := FormulaInfo{}
f.init(inputfile)
f.getNeededFunc(formulae)
notsameway := []string{}
callerNeeded("main", callMap, neededFuncMap, notsameway)
f.callerNeeded("main", notsameway)
blackbox := ""
for key, val := range neededFuncMap {
for key, val := range f.NFunc {
if !val {
blackbox += key + ";"
}
}
return blackbox
return blackbox, f.NVariables
}
func getNeededFunc(formulae string, neededFunc map[string]bool) {
func (f *FormulaInfo) getNeededFunc(formulae string) {
input, err := os.Open(formulae)
if err != nil {
fmt.Println(os.Stderr, err)
......@@ -67,35 +70,35 @@ func getNeededFunc(formulae string, neededFunc map[string]bool) {
scanner := bufio.NewScanner(input)
r, _ := regexp.Compile("\".*?\"")
for scanner.Scan() {
parseFormulaeLine(scanner.Text(), r, neededFunc)
f.parseFormulaeLine(scanner.Text(), r)
}
}
func parseFormulaeLine(line string, r *regexp.Regexp, neededFunc map[string]bool) {
func (f *FormulaInfo) parseFormulaeLine(line string, r *regexp.Regexp) {
lineList := r.FindAllString(line, -1)
for _, elt := range lineList {
elt = elt[1 : len(elt)-1]
checkVarName(elt, neededFunc)
f.checkVarName(elt)
}
}
func checkVarName(elt string, neededFunc map[string]bool) {
for key, _ := range neededFunc {
neededFunc[key] = strings.Contains(elt, key)
func (f *FormulaInfo) checkVarName(elt string) {
for key, _ := range f.NFunc {
f.NFunc[key] = f.NFunc[key] || strings.Contains(elt, key)
f.NVariables[strings.Fields(elt)[0]] = true
}
}
func callerNeeded(currentFunc string, cg map[string]*FuncInfo, neededFunc map[string]bool, notsameway []string) bool {
needed := neededFunc[currentFunc]
func (f *FormulaInfo) callerNeeded(currentFunc string, notsameway []string) bool {
needed := f.NFunc[currentFunc]
if contains(notsameway, currentFunc) {
return needed
}
notsamewaybis := append(notsameway, currentFunc)
for _, elt := range cg[currentFunc].Calls {
needed = callerNeeded(elt, cg, neededFunc, notsamewaybis) || needed
for _, elt := range f.CallMap[currentFunc].Calls {
needed = f.callerNeeded(elt, notsamewaybis) || needed
}
neededFunc[currentFunc] = needed
f.NFunc[currentFunc] = needed
return needed
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment